Getting Started with .NET Entity Framework

Entity Framework is one of my Micrsoft's new recommended ways to build a data access layer. It provides a lot of flexibility in allowing you to control your database model, easily update your model, and automatically generates a wrapper on your database model so that you can access all of your tables as objects (called Entities).

Entity Framework supports LINQ so it makes it really easy to visually design your database model and then immediately start writing code to access your data using LINQ statements. It feels very similar to the older versions of LINQ to SQL (which is currently supported still by Microsoft but appears to be inevitable that LINQ to SQL will be superceded by Entity Framework sometime in the near future).

The following is a quick list of the steps to get Entity Framework set up in your project and some examples of how to use it.

First, I have set up a simple database model as an example. Here is what the model looks like in SQL Management Studio's Database Diagram tool:

In your Visual Studio 2010 solution, choose the project you want to put your database model in. Then right-click the project and choose Add -> New Item. From the list that comes up, choose ADO.NET Entity Data Model.

Choose Generate from Database Model to get the nice automated object wrappers for your model.

Choose or build the connection to your database and the naming rules you would like to use.

Select the tables, views, and stored procedures you want to be added to your model. The items you choose here will be what is available in your code after this wizard is completed.

After clicking finish, the UI will refresh for a few seconds and then you should see your tables and other objects laid out on the model with all relationships already in place.

Now you can start making calls against your database. You will be using LINQ against your entities so in the project you want to access these entities from, you may need to set a reference to System.Data.Entity (if not already done for you).

Here are some simple examples that show how easy it is to access your tables and even do joins and sub-queries as well as inserts, updates, and deletes.

Select

// create instance of entity framework model context to track changes and do queries with
Data.testEntityFramework context = new Data.testEntityFramework();

// select all
var listOfUsers = from q in context.UserAccounts
                    select q;

// select with simple where clause
var listOfEmails = from q in context.UserAccounts
                    where q.Email == "some@email.com"
                    select q;

// select and get joined table
var listOfRoles = from q in context.UserRoles
                    select q;
foreach (var role in listOfRoles)
{
    // get user of this role by relationship
    string userFirstName = role.UserAccount.FirstName;
}

Update

// create instance of entity framework model context to track changes and do queries with
Data.testEntityFramework context = new Data.testEntityFramework();

// update
var updateUser = (from q in context.UserAccounts
                    where q.Id == 1000
                    select q).Single();
updateUser.FirstName = "John";
updateUser.LastName = "Smith";
context.SaveChanges();

Insert

// create instance of entity framework model context to track changes and do queries with
Data.testEntityFramework context = new Data.testEntityFramework();

// insert
Data.UserAccount newUser = new Data.UserAccount();
newUser.FirstName = "Bob";
newUser.LastName = "Johnson";
newUser.Email = "bob@johnson.com";
newUser.DateCreated = DateTime.Now;
context.UserAccounts.AddObject(newUser);
context.SaveChanges();
// get the newly assigned row primary key
int newId = newUser.Id;

Delete

// create instance of entity framework model context to track changes and do queries with
Data.testEntityFramework context = new Data.testEntityFramework();

// delete
var deleteUser = (from q in context.UserAccounts
                    where q.Id == 1000
                    select q).Single();
context.UserAccounts.DeleteObject(deleteUser);
context.SaveChanges();

If, in the future, you ever want to update your model, just right-click anywhere on the entity framework designer workspace and choose Update Model from Database. This is a REALLY NICE feature because in LINQ to SQL, you usually had to delete tables and re-add them and if you had ever made customizations, they would be lost. Entity Framework makes this much easier and faster.

AttachmentSize
screen1.jpg22.7 KB
screen2.jpg107.93 KB
screen3.jpg56.98 KB
screen4.jpg85.61 KB
screen5.jpg63.45 KB
screen6.jpg39.38 KB
screen7.jpg36.88 KB
screen8.jpg23.83 KB