Sunday 19 October 2014

Choosing the Right Entity Framework Workflow

In this article we will learn what is the advantages and disadvantages of each domain modelling approaches and when to use which approach. Here you will find what are the different approaches we use in Entity Framework.

When to use which Model: 

It is very important to know which work flow or approach we need to use for our application development. Some times it depends on developers interest but most of the times it depend on the project structure. Let's discuss which should be our preferable approach in different scenarios. 

(a)Database First approach shines when managing a large, complex database schema with numerous unconventional mapping requirements or if we are working with an existing project (maintenance project) and need to mapping to an existing database. Here we generate a model from that existing database.

(b)If we are working with a new project which don’t have a database and want to use a designer to define the shape of the classes and define a structure to map to the database then its preferable to use Model First approach. So using this approach we can create the database based on the model we defined. Here powerful features of the ORM designer help in creating a model very easily.

(c)Then coming to the last one i.e Code First approach. If we want to write our own POCO classes and use code to define how they map to a database then its preferable to use Code First approach. Code First can generate a database for us or we can use it to map to an existing database. Hard code coders love to use this approach.

(d)Finally, situation may comes where we have existing classes and we want those classes to be used with EF,then its always preferable to use the code first approach even if our first preference would be designer-based modelling because if we will choose to use the designer, then for any model changes in the designer we need to change our classes also. Which is inefficient and error-prone, so we will probably be happier in the long run if we use Code First. In Code First, our classes are our model, so model changes only need to be made in one place and there is no opportunity for things to get out of sync.


Before committing to a particular work flow a developer/system designer must go through the mentioned decisions within the above decision tree.(By Julie Lerman the author of "Programming Entity Framework")

Now Coming to the Advantages and disadvantages of each approaches:

Database First approach:


Advantages:

  1. Famous if we have Database which is developed separately or designed by a good database designer or if we have an existing Database. 
  2. EDM wizard creates entities, relationships, and inheritance hierarchies for us. As code is auto generated so it decreases the overhead of writing code. 
  3. Manual changes to the database is possible here as we generate model from the database. We can always update the model from database. 
  4. We can visualize the database structure in EDMX designer. 
  5. We can add extra features in POCO entities by using either T4 template or partial classes.

Disadvantages:

  1. If we will change anything in model then it won't be reflected in Database.  
  2. In case if there is a problem while sync the Database with the model then there may be a chance of deletion of the database and fear of losing data.

Model First Approach: 


Advantages:

  1. It is good if we like to visualize the structure of the data in the application or we don't like writing code or SQL . 
  2. Good support with EDMX designer. 
  3. If we want to add some extra features in POCO entities we can achieve this using partial classes. 
  4. We can modify the model and update the generated database. 
  5. For small easy projects this approach is very productive.

Disadvantages:

  1. In this approach you have no much control over your entities (auto generated code which is hard to modify) and database. So in this case it is rarely used. 
  2. Manual changes to database schema is not preferable as entityFramework model defines the database. 
  3. When working on a team Model First can sometimes be a pain. This is true in initial development when changes are frequent to the database.

Code First: 


Advantages:

  1. It is very popular approach since it allow us to make a more logically and flexible application. 
  2. It provides full control over the code as there is no auto generated code.
  3. No manual intervention to DB is required as Entity Framework will handle creation of database with its relations. (If DB is not exist previously)
  4. Same class is used for defining the database table structure and business object. So any changes in the class affect both the business object and the database table.
  5.  We can also use Code first to map our model to an existing database.  

Disadvantages:

  1. Manual changes to database schema is not preferable because our code defines the database. 
  2. Generated code is not always easy to understand.
 
Hope this will help you. :)

No comments:

Post a Comment