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.

| Attachment | Size |
|---|---|
| screen1.jpg | 22.7 KB |
| screen2.jpg | 107.93 KB |
| screen3.jpg | 56.98 KB |
| screen4.jpg | 85.61 KB |
| screen5.jpg | 63.45 KB |
| screen6.jpg | 39.38 KB |
| screen7.jpg | 36.88 KB |
| screen8.jpg | 23.83 KB |
Popular Articles
Last viewed:
- Get the list of ODBC data source names programatically using C#
- Fix for Visual Studio 2008 SP1 Design Mode Error - [text] could not be set on [property]
- Using Nullable Data Types with C#
- SQL Create Table Add Description to Column
- Windows Server 2003 Configure RRAS (Routing and Remote Access Service) Site to Site VPN
- Building a Web Service in ASP.NET 3.5
Similar
- Tutorial for Configuring Silverlight 4, Entity Framework and WCF RIA Services in Separate Component Assemblies (DLL’s)
- Fixing Relative Paths in C# ASP.NET When Using Url Rewriting
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- Global.asax Events in IIS 6 and IIS 7 for Static Resources
- Create a Windows-style GroupBox in ASP.NET

Recent comments
5 days 11 hours ago
1 week 1 day ago
1 week 1 day ago
2 weeks 4 hours ago
2 weeks 1 day ago
2 weeks 2 days ago
2 weeks 4 days ago
2 weeks 5 days ago
3 weeks 16 hours ago
3 weeks 1 day ago