May 22, 2013

Approaches in Entity Framework

Entity Framework is preferred method of Microsoft  to access data in NET applications. It supports strongly-typed access through LINQ and allow  developers to program against a conceptual model that reflects application logic rather than a relational model that reflects the database structure. 

There are three types of approaches in Entity framework.
  1. Code First
  2. Model First
  3. Database First

Code First:

In Code First approach, it provides a new development pattern that provides an alternative approach to Model First and Database First and create databases of us based on our classes. 

In this approach, there is no EDM at all and the database is generated from the data access code that you write. With this you write the code you want as plain classes, then the EF models are inferred from that at run-time. These models are used to generate the database, if you like, as well as provide the mapping from your hand-written classes. Just because you can’t see the EDM though doesn’t mean it’s not there. The metadata is still created under the covers and the code you write is used to create it at runtime. You should note that this approach is only enabled by the downloadable CTP 'add on'.

Model First:

In Model First approach, where you have a clean slate to start and freely able to use expressive power to design whatever you like. You can use this feature using EF Designer.

If you have a pre-existing database or would like to design the database first exactly how you’d like it, you can import the database into EF and the EDMX will be created from it. This will include all the usual metadata, CSDL, SSDL and the mapping between them just as model-first does. However it’s likely that you will want to change the model once you have imported the database to better map the database tables onto the entities and hierarchical relationships in your model. With this it cannot be inferred what the original model’s hierarchy and structure was and so the generated model can be tweaked accordingly to your taste. 

Database First:

In Database First approach, it provides an alternative to the Code First and Model First approaches to the EDM and it creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.

Hope you understand well all these concept.