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. :)

Saturday 18 October 2014

Various Domain modelling approaches in Entity Framework

Every business application has a conceptual data model (explicit or implicit) which describes the various elements of the problem domain as well as each element's structure, the relationships between each element and their constraints.We follow different approaches to deal with these models. Entity Framework also provides three different approaches to deal with the model.

  1. Database First 
  2. Model First 
  3. Code First

Let's discuss these approaches one by one.

Database first approach: 
This is a approach where we created the model codes (classes, properties, DbContext etc.) from an existing database. We achieve this by using Visual Studio’s Entity Data Model Wizard . This approach first comes with Entity frame work 3.5 (Visual Studio 2008 ).


Model First Approach: 
This is a approach where we create Entities, relationships and inheritance hierarchies directly on the design surface of empty model by using entity designer (.edmx file) and then generate database from it. This approach first comes with Entity frame work 4.0. (Visual Studio 2010 ).


Code First Approach: 
This is a approach where we create the classes first and then create database directly from these classes. These classes are called POCO (Plain old CLR objects) classes. In code first, we never use the Entity Designer (for editing .edmx files) rather what we use is the POCO classes. This approach allow us to make a more logically and flexible application that focuses on the behavior of the application rather than the database generated by it. We can also use Code first to map our model to an existing database. This approach first comes with Entity frame work 4.1 (Visual Studio 2010 ).

If you want to know when to use which approach and what is the advantage and disadvantages of the above approaches then you will find it here.

Hope this is helpful. :)

Friday 17 October 2014

How to create a entity data model using database first approach.

Here we will learn how you can create a entity data model from an existing database (Known as database first approach). Please refer this to know what is a database first approach.

Create an Entity Data Model In a Separate project : 
Below are the steps to create a data model using database first approach (EF 4.5 VS 2012):

Step 1: Open Visual Studio 2012 > File > New Project > Windows Template > Class Library > Give it a name 'EntityFrameworkModel' > OK


Delete the default Class1.cs that gets created in the project. 

Step 2:Right click on the project in Solution Explorer > Add > New Item Select the Data Template > ADO.NET Entity Data Model and click Add.


In the Entity Data Model Wizard, choose 'Generate From Database' and click Next.


Create a new Data Connection or choose from an existing one. If you want to change the database connection then click on New Connection. A popup will open.


Select the Server name.You can either choose windows authentication or SQL server authentication.In SQL server authentication you need to provide the username and password. Then select the database name you want to connect and click on test connection. If it will show a successful message then click on OK button and proceed.


Step 3: Now choose the Database Objects you want to include in your Entity Data Model (EDM). Here I choose all the four tables 'Department' ,'Employee' ,'LogInDetails' and 'Salary'. As i dont have any stored procedure and view in my database so am not going to select any SP and view.If you want to change the name of the model you can change it under model namespace textbox. Click Finish to complete the wizard.


Then you will able to see the .edmx file as below.


You will find the connection string in the app.config file where you will find find the schema files (.csdl, .mls, .ssdl) :


Build the project in Release mode (Go to Build > Build EntityFrameworkModel) and make sure that there are no errors. A model defined by the Entity Data Model is now ready.

If you want to know how to use this entity data model in another .Net application to communicate with the database, then here you will find the link which will help you to achieve this.
 

Hope this is helpful. :)