Entity Framework 4.1 first steps

I use NHibernate since its first versions and I really love it :), but after Entity Framework 4.1 is out, I started to consider using EF for people that actually does know anything of ORM and still work with dataset or use handwritten SQL CRUD, but could not use open source project or prefer using MS technologies.

I tend not to agree with this path, but sometimes it is easier to tell people, “hey, just install this update of Microsoft and you can use EF code first, or if you want a designer you can simple add a new EF Model to the project”. People tend to consider less work using something that is already included in the framework, instead of going toward some external open source library. I must also admit that using EF 4.1 is quite fun (I used in really little projects, but 4.1 version gave me a good impression).

If you want to use EF4.1 in your application you need to do very little steps, suppose you have this class to persist.


You need to reference the Ef4.1 assembly and then create a Context to be able to persist this class.

   1: public class BattlefieldContext : DbContext

   2: {

   3:     public DbSet<Warrior> Warriors { get; set; }

   4: }

This is really simple, just create a class named BattlefieldContext, inherit from DbContext, then insert a public property of type DbSet<YourEntityThatYouWantToPersist>.

Now go to app.config (or web.config) and create the connectionstring to the database with the same name of the context class (thus it is named BattlefieldContext)

   1: <connectionStrings>

   2:   <add

   3:     name="BattlefieldContext"

   4:     providerName="System.Data.SqlClient"

   5:     connectionString="Server=localhost\sql2008;Database=Battlefield;Trusted_Connection=true;"/>

   6: </connectionStrings>

Now write the code that insert a Warrior into the database.

   1: Warrior Raistlin;

   2: using (var db = new BattlefieldContext())

   3: {

   4:     Raistlin = new Warrior { Name = "Raistlin", ExperiencePoints=342553 };

   5:     db.Warriors.Add(Raistlin);

   6:     db.SaveChanges();

   7: }

Run these few lines of code, everything works, but wait…. you never created the database but no exception occurs. This happens because the current user has administrative rights on the Sql Server, so, after the connection to the database engine was opened, EF41 verified that the Battlefield database did not exists, so he created one to persist the Warrior class.


Generation of the database is straightforward, EF41 generates a table with a column for each property of the object that needs to be persisted, and he got special care for property named Id, because it is the primary key in the table. Since the Id property is an integer, the default convention creates a primary key with Identity Specification turned on. If you read all rows in the table you can verify that the warrior Raistlin was really saved into database.


You should admit that using EF41 in such a simple scenario is damn simple and efficient, very few lines of code and you are ready to use it.



Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

2 thoughts on “Entity Framework 4.1 first steps”

Comments are closed.