Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles. Follow galratner on Twitter Google
Four ways to utilize Entity Framework 4

There is some confusion out there about a simple question: How can I use Entity Framework? Some developers have heard about reverse engineering models so I decided to write a quick preview to the main features of EF4. This isn’t an in-depth article, but, rather a visual summary of the main four scenarios EF can accommodate.  If you are looking to utilize any of the scenarios described here you might need to seek further documentation and realize that development with Entity Framework is a vast subject and is beyond the scope of a single article.


Let’s explore the scenarios available to us:

Database First
The most common scenario is starting with an existing database and generating a model from it. I have created a small database with two tables, one for customers and the other for orders. There is a one-to-many relationship between a customer and the orders that can belong to a customer

 

.


 
 
We can begin by adding a new ADO.NET Entity Data Model


 
The Entity Data Model Wizard will ask us to choose a Model Contents. Lets select Generate From Database

.


 
We can select an existing database connection or define a new connection.

 
Finally the Wizard will ask us to select the objects we want the model to contain: Tables, Views and Stored Procedures.

 
When we click finish the wizard will create the new model. We can see here that both of our tables are now in the model.

 
Model First
In a Model First approach we will create a model first and then generate a database from it.  Let’s begin by selecting an empty model from the Entity Data Model Wizard.

 
An empty design surface will show up, allowing us to select and add Associations, Entities and Inheritance from the toolbox on the left. When we are done designing our model we can click anywhere in the design pan and select Generate Database from model.

 
The Generate Database Wizard will open and show the SQL that is about to run in order to create our new database. Here we can fine tune the create statements in order to optimize columns, indexes etc.

 
Here is the final SQL about to run on our new database

 

Model with POCO
POCO (Plain Old CLR Object) can replace your entities and are useful when you need more control over your data objects. For example if you are returning a data object from a WCF and need to serialize some of the fields you can add a POCO object with data member attributes on the properties you need to return. Although you can code your POCOs yourself it will be significantly easier to use a POCO Generator. You can download the ADO.NET C# POCO Entity Generator from your Visual Studio’s Extension Manager. The generator is T4 (Text Template Transformation Toolkit) based and will create the .tt files in your solution. As a first step, search and install the generator using the Extension Manger.

 
Then right click on your model and select Add Code Generation Item

 

Select ADO.NET POCO Entity Generator.


You will get some security dialogs while the templates are generating your POCO classes. The end result will be a class for each model entity and a class for the Context. There are some gotchas with POCOs. The syntax might differ and you can’t mix POCO and regular models. Performance will too suffer as Entity Framework creates some background proxy objects.


 
This is an example of a generated POCO:

 

Code First
The last use case is hard coding both the Context and the entities to create or use a database structure.  In a Code First case we can generate a database whenever the model changes, make changes to an existing database or simply use an existing database. This option differs from model with POCO in a few areas; since the model is able to make changes to dataset, the entities are more tightly coupled with database tables. Primary and foreign keys can be defined as attributes in the entity classes or using the Fluent API.
To use code first we need to download the RC release of EF 4.1 using NuGet’s supported package sources. We are going to select and install the library EntityFramework which contains the RC release.

 
After we have done that and NuGet has added the necessary references to our project we need to code the context to match our existing database. We are going to inherit from DBContext and since we are using an existing string connection we added a constructor to accept the connection name if the string connection matches the model name, ET will automatically use it. In our case we set it ourselves.

 
Now we can code the individual entity classes

 
 
Putting everything together looks like this:

 
We can tell that the model followed our foreign key constraint as we defined in the attributes.


Conclusion
This has been a very short summary of all of the current options available to you when working with Entity Framework 4. I did not go into details for each method as there is plenty of documentation out there. I hope you find this article useful when deciding on development strategy with Entity Framework.

Shout it


Posted 24 Mar 2011 6:28 AM by Gal Ratner
Filed under:

Powered by Community Server (Non-Commercial Edition), by Telligent Systems