Please enable Javascript to correctly display the contents on Dot Net Tricks!

Various Domain modelling approaches in Entity Framework

  Author : Shailendra Chauhan
Posted On : 20 Mar 2014
Total Views : 30,631   
 

A business domain is populated with related and interconnected entities which have its own properties and behavior. Most important thing is that each entity may have a state and can be bound to a possibly dynamic list of validation rules. EF allow developers to focus on the business domain and to model it in terms of classes.

Domain modelling approaches

There are three approaches of domain modelling which was introduced with Entity Framework 4.1

  1. Code First

  2. Model First

  3. Database first

Code First

Code first is the domain modelling approach in Entity Framework. It enables you to describe a model by using C# or VB.NET classes and then create database from these classes. These classes are called POCO classes.

This approach enables us to work entirely in an object-oriented direction, and not worry about the structure of the database. This abstraction allow us to make a more logically and flexible application that focuses on the behaviour of the application rather than the database generated by it.

Advantages of Code First

  1. It is very popular approach since it allow you to make a more logically and flexible application.

  2. It provides full control over the code since there is no auto generated code which is hard to modify.

  3. In this approach, your code defines only the database mappings and EF will handle creation of database with its relations.

  4. Manual changes to database schema is not preferable because your code defines the database.

  5. You can also use Code first to map your model to an existing database.

Model First

Model first is the domain modelling approach in Entity Framework. It enables you to create model’s Entities, relationships, and inheritance hierarchies on the design surface of empty model (.edmx file) by using entity designer and then create database from it. This approach is adopted by the architect and solution lead developers.

In Model First approach, while creating Entity Data Model, you must select the option “Empty Model” instead of "Generate from database" option.

Advantage of Model First

  1. It is good if you like to visualize the structure of the data in the application or you don't like writing code or SQL since it will generate for you.

  2. In this approach you have no much control over your entities (auto generated code which is hard to modify) and database. In this way it is rarely used but for small easy projects this approach will be very productive.

  3. To add extra features in POCO entities you must either T4 modify template or use partial classes.

  4. Manual changes to database schema is not preferable because your model defines the database.

Database First

Database first is the domain modelling approach in Entity Framework. It enables you to create model from an existing database (like SQL Server, Oracle, DB2 etc.). This approach reduces the amount of code that we need to write since it automatically generates code. But it also limits us to work with the structure of the generated code.

Advantage of Database First

  1. Very popular if you have Database designed by DBAs, developed separately or if you have existing Database.

  2. EDM wizard creates entities, relationships, and inheritance hierarchies for you. After modification of mapping, you can also generate POCO class entities.

  3. To add extra features in POCO entities you must either T4 modify template or use partial classes.

  4. Manual changes to the database are possible because the database defines your domain model. You can always update model from database.

What do you think?

I hope you will enjoy the domain modeling approaches while programming with EF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
6 JUL
ASP.NET Core with Angular4 Development (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/T

Know More
5 JUL
NodeJS with Angular4 Development (Online)

08:00 PM-09:30 PM IST(+5.30 GMT)M/W/F

Know More
1 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM-05:30 PM IST(+5.30 GMT) Weekend

Know More
1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

8:00 AM-10:00 AM Weekend

Know More
1 JUL
ASP.NET Core with Angular4 Development (Classroom)

3:00PM 5:00PM Weekend

Know More
1 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM 5:00 PM Weekend

Know More
1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

Know More
30 JUN
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5.30 GMT) MON/WED/FRI

Know More
29 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/W/T

Know More
26 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

Know More
26 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)(TT)

3 JUN
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:30 PM-7:00 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+