Code First in Entity Framework

There are two .Net ORM approaches natively supported by Microsoft: Linq2SQL and EntityFramework. While the future of the Linq2SQL is unknown EntityFramework is being actively developed.

EntityFramework provides a whole bunch of advanced ORM features including a large variety of supported mappings, a useful model designer, and auto-generation of model classes. But a lot of developers don’t like all this heavy stuff, which requires careful separation of auto-generated code from custom code, and dealing with a visual editor and huge XML files, which is in fact less productive for developers than simple code writing.

That is why, starting from EF4.1, the Code First approach appeared alongside the DB First and Model First approaches, as a natural resolution of the problem.
This approach enables writing simple POCO (Plain Old CLR Objects) model classes first and then connecting those POCO classes with the database very easily by using the the DbContext class.

References to other POCO objects and collections are recognized automatically as object relations including many-to-many. To override and extend standard mapping rules, Fluent API or/and Data annotations are used.

A lot of developers also find Code First approach very useful in case of multiple DB support – you don’t need to regenerate any models – just use a new connection to the DB.

Recently Entity Framework 5.0 has been released. This version provides some performance enhancements and a list of new features, such as support of enums and table-valued functions (only available when writing an application that targets .NET 4.5). Mapping to stored procedures is still not supported.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *