Introducing Code First Entity Framework 4.0!

The ADO.NET team recently came up with a new concept in OR mapping called “Code First” model in Entity Framework 4.0

In addition to supporting a designer-based development workflow, EF4 also enables a more code-centric option which we call “code first development”.  Code-First Development enables a pretty sweet development workflow

While using the Entity Framework, how many times did you need to completely re-create your data-context files just because you changed the data model a tiny bit? And how much time did that waste?…Well if you end up changing your data context frequently and if the database structure is NOT the core focus of your application (i say that because if the Database is VERY complicated it MAY be easier to make the database first anyways), then Code First EF is to your rescue…

Put in simple words:- CF EF 4.0 allows you to

  • First define your data classes as POCO (Plain Old Classes) without any overhead attributes etc
  • Use your POCO classes with a Datacontext just like you would use the classes generated via the Add New Item–>Add Entity Data Model class
  • Generate your database out of the POCO classes by simply specifying a connection string.

Nothing can be simpler than this and no better way to explain this than an example…

I have an MVC app which enabled CRUD operations on an object called “Idea”.

Step1:- Download and installed EF CTP( currently CTP5)

Step1:- Create an IdeaModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace IdeaMVC.Models
{
    public class IdeaModel
    {
        public int IdeaModelID { get; set; }
        public string IdeaTitle { get; set; }
        public string Description { get; set; }
        public string SubmittedBy { get; set; }

    }

}

As you can see its a POCO model without any decorations

Step2:- Add References

Add Reference to EntityFramework.dll and System.Data.Entity.dll from the .NET tab

Step3:- Create the Datacontext

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace IdeaMVC.Models
{
    public class IdeaContext:DbContext
    {
        public DbSet<IdeaModel> Ideas {get; set;}
    }
}

DbSet and DbContext are from the System.Data.Entity namespace and this will create the datacontext from where you can query your ideas using your favorite Linq to Entities!

Step4:- Define where to store the data

This is where you dictate where the data will reside. Place the connection string in the config file

<configuration>
  <connectionStrings>
    <add
      name="IdeaContext"
      providerName="System.Data.SqlClient"
      connectionString="Server=.\SQLEXPRESS;Database=Products;Trusted_Connection=true;"/>
  </connectionStrings>

The current connection string will create a database called Products  in the local SQL Server if it doesn’t already exist. Your connection string could very well have pointed to an attached DB or SQL CE

A few important points here

  1. The name of the connection string should be the name of the Datacontext (which in our case is IdeaContext and hence the connection string is also called IdeaContext)
  2. If you want to specifically rename the connection string you can do that by passing the new connection string name to the base DataContext constructor . For Example
    public IdeaContext():base("MyConnectionString"){}

  3. If the database is not present at the specified location it will be specifically created based on the DataModel
  4. If the database is already present its schema is matched to the existing datamodel schema to check for updates.

Creating the database from the datamodel follows some rules . For example, it tries to identity the PK as the name of the datamodel+ID so in this case it recognizes the IdeaModelID as the PK. If your PK is defined differently you can specify a [Key] attribute  from the .NET 4.0 System.Component.DataAnnotations namespace to indicate the PK.

Many configurations are possible but for now lets take the most basic case and continue.

OK so we defined the model, the datacontext and the connection string…Next we need to use the datacontext…

Since i am using the MVC model, i will access my list in the Index method  (Note that this can be used in any .NET app)

So my Index method looks like this

 IdeaContext context= new IdeaContext();
 var j = context.Ideas;
 return View(j.ToList());

 Lets also take a quick look at the Create method which contains the following code

  [HttpPost]
        public ActionResult Create(IdeaModel  idea)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    idea.IdeaID = new Random().Next();
                    IdeaContext context = new IdeaContext();
                    context.Ideas.Add(idea);
                    context.SaveChanges();
                    return View("Details", idea);
                }
                else
                {
                    return View("Create", idea);
                }
            }
            catch (Exception ex)
            {
                return View();
            }
        }

So the datacontext usage is the EXACT same as in a Database first approach..

Now lets take a look at my SQL Server DB list to ensure i am not bluffing you:-)

image

So there is no database called products here…

Now lets run the application

image

Click Create New to create the new Idea

image

Now lets check the database again

image

Along with the Products DB with the expected IdeaModels table, you see one more table called EdmMetadata….This table is responsible for monitoring the mapping between the code schema and the database…if you change the code schema without changing the DB schema you will get an exception…There are three options here

1. Manually make the changes in the DB schema also

2. Manually Delete the database and run the code to recreate it

3. Auto -Recreate the DB by Adding

DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<IdeaContext>());

at the application startup. My app being an MVC model i added this at the Application_Start()

This was a very brief introduction to a very easy and extremely useful approach to working with Databases..

I found the following two blog posts most helpful in this context…so do go through them in details.(in the order given as Scott Gu’s blog is of CTP 4 so some terms changed in CTP 5 which is the current CTP as of the time this blog post was written!)

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx

Hope this is helpful!

Cennest