Geeks With Blogs

Lance's TextBox

Here’s a quick start introduction to the ADO.NET Entity Framework Code First Development Pattern.  For a more complete (but still intro) walkthrough, check out ScottGu’s blog.

At the time of this writing, you’ll need the EF 4.1 Release Candidate.  After EF 4.1 is released you’ll no doubt be able to find it on the ADO.NET team blog.

1.  Create a new empty ASP.NET MVC2 Web Application, add a reference to System.Data.Entity.  If using the EF4.1 RC - add a reference to the EntityFramework dll that comes installed with it.

2.  Add the model classes you’d like to represent your data objects.  For example if you need to represent game nights, you might create a class called Game with properties that describe the game night date, location, and other information.

3.  Create a “Context Class” that inherits DbContext, which contains nothing but DbSet<yourclassnamefromstep2> public properties.  For example if you created in step 2 a class called “Game”, here you’d have a property defined as:

public DbSet<Game> Games { get; set; }

At this point, EF code first will make certain assumptions (which we can override later if necessary) that allow us to write less code:

- The context class will map its properties (those DbSet properties) to tables with the same names (a Games table, in our example).

- Each row in that table will map to an instance of the model class (a Game instance, in our example). 

- Properties in the model class are columns in the table.

- A property in the model class with the name “Id” or “<classname>Id” will be made the primary key.

- Relationships between tables are inferred if one model class has a property with the name “<otherclassname>Id”.

4.  Add a controller to see EF Code First in action.  For example, add Index (to just return a list of our game nights) and Create actions (to create a new game night).

5.  Add the views (Index: strongly typed IEnumerable of Game, Create: strongly typed Game – content Create).

6.  Create the database.  One of those EF Code First assumptions is that it will automatically look for a connection string with the same name as the context class (eg, Games).  Scott Guthrie recommends the use of SQL CE 4 for early development, and moving to SQL Server or SQL Express closer to the time of deployment.

Important:  IF you’re using SQL CE or SQL Express, and your connection string points to a database file that does not yet exist, it will be automatically created for you!

Updating The Model

If you update the model (lets say we add a new property to our Game class), you’ll get an error the next time you attempt to run the application (“context has changed since the database was created”.  EF Code First can handle this too:  in Global.asax, add the following to the Application_Start event handler:

Database.SetInitializer<<yourContextClass>>(new RecreateDatabaseIfModelChanges<<yourContextClass>>());

Gu gives a nice tip – create a custom class that implements RecreateDatabaseIfMOdelChanges<<yourContextClass)> and overrides the Seed function to add “seed” rows to the database with default data.  This way when we recreate the database after a model update, the database isn’t empty – but is seeded with our default data.  Use this new custom class in the call to SetInitializer instead of RecreateDatabaseIfModelChanges.

Posted on Thursday, April 14, 2011 3:42 AM Programming | Back to top

Comments on this post: ADO.NET Entity Framework – Code First Development Pattern

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Lance Robinson | Powered by: