Book Review: Programming Entity Framework - Code First
- Title: Programming Entity Framework: Code First
- Author: Julia Lerman, Rowan Miller
- Level: Beginner to Intermediate
- Publisher: O'Reilly Media
- Pages: 176
- Rating: 4 of 5
EF 4.0 provides two approaches for working with entities, viz. Database First
and Model First. In the former case you design a database before starting your
application development. Then you use Entity Data Model Designer of Visual
Studio to design a data model visually by dragging and dropping tables from the
database onto the designer. In the case of Model First development you use
Entity Data Model Designer to design data model classes manually. You then
specify the database connection to be used in the application. In any case you
use Entity Data Model Designer to generate your data model classes. This
approach though useful at many places has its own shortcoming. The "visual designer"
based approach taken by Entity Framework
doesn't take care of existing data access classes (POCO style). You either need to drop them altogether
and re-create them using the designer or use EF
only for new development. Luckily, Microsoft introduced Code First as a supplement to
the existing EF infrastructure that can be of
great use in such scenarios. As the name suggests Code First approach skips the
"visual designer" altogether, rather you code your classes (or use existing ones
as a base) and then make them EF aware by applying attributes and such stuff. If
this sounds interesting to you then probably you should pick
Programming Entity Framework: Code First by Julia Lerman and Rowan Miller.
The book provides a concise understanding of Code First in 176 pages, yet
it's not a pocket reference kind of book. Diagrams, screen shots and code
snippets are scattered throughout the book. The book consists of eight chapters.
The first two chapters provide an introduction to Code First. Firstly, they
give you idea as to what you can do and what you can't with Code First.
Secondly, you develop a complete example that illustrates many pieces of Code
First such as data annotations and fluent API.
Chapter 3 discusses in detail the conventions and configurations for property
attributes. These are the attributes that you use to decorate class properties
to mark them as primary key, length, nullability and so on. It also covers how
data types such as time stamp and decimal can be represented and ends with
information on representing complex types.
Chapter 4 talks about conventions and configurations for relationships.
Topics covered include multiplicity, foreign keys and inverse navigation
properties. Further, it shows how to deal with cascade deletes and how to work
with Many-to-Many and One-to-One relationships.
Chapter 5 is devoted to conventions and configurations for database mappings
and covers many aspects of how Code First deals with database and tables. After
covering basics such as mapping class name with table and property names with
columns it explains scenarios such as mapping multiple entities to one table,
mapping one entity to multiple tables, preventing properties from getting
included in the model and mapping inheritance chains. The chapter concludes with
discussion of controlling the foreign keys.
Chapter 6 covers various aspects related to database in terms of its creation
and location. It also discusses how to seed sample data during database
Chapter 7 deals with advanced scenarios such as mapping to database objects
that are not tables (Views and Stored Procedures). Further, it talks about
overriding the default behavior of model caching and working with Entity Data
Model Metadata table (EdmMetadata).
Chapter 8 concludes the book by pointing to the future of Code First and
talks in brief about Code First Migrations and Entity Framework Power Tools.
To summarize, Programming Entity Framework: Code First is a neatly written
and concise coverage of all the important aspects of Code First development. If
you are a developer looking to develop data driven applications using Code First
technology then this book will be a great choice. If you wish to master the
overall EF concepts (not just Code First) I would also suggest you to read
Programming Entity Framework also by Julia Lerman.