Sep 07

First attempts with MVC2 and EF4

I’ve recently started looking at skilling up in MVC2 and Entity Framework (EF), after last doing some web dev using web forms … quite a while back. I stumbled upon some great MVC2 presentation videos with Scott Hanselman. Here they are: Basics-Introduction and Ninja-Black-Belt-Tips. These made me quite enthusiastic about starting my own little MVC2 project, and I’d like to share with you some more information on the code-first approach and the issues I encountered initially.

Getting started with the code-first and POCO approach, I found ScottGu’s post to be very helpful. You’ll need to download the EF CTP4 from Microsoft.
I spent a good amount of time writing my POCO classes before moving onto the DB side. Instead of SQL CE 4 I opted for SQL Server 2008 R2 Express, which worked just as well once I realised that the user connecting to SQL needs create database rights. EF will create the database that you specify in the connection string if it does not exist.

Thinking I was good to go after the inital DB hurdles, I fired up the app again only to be greeted by an unspecific error:

The entity type <blah> is not part of the model for the current context.

EF had clearly created a table for the type. The type’s properties were all correctly mapped to their respective columns. Well, I finally found out why: my type had an enum property. Though it had been correctly mapped, it seems EF4 and enums just don’t play nice together at the moment. So that’s what that error message means.

You can get around it by wrapping your enum property as described here and here, though hopefully that won’t be necessary in the near future. If you follow that second link you’ll notice that Alex talks about creating a ComplexType. I couldn’t figure out where or how to do that just by that piece of XML he put in his post. Eventually I found out that you can do this by overriding OnModelCreating in your DbContext class. As in:

    public class SmsDbContext : DbContext
        public DbSet<Team> Teams { get; set; }
        public DbSet<Player> Players { get; set; }
        public DbSet<Message> Messages { get; set; }

        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)

Here you can see my db context class, inheriting from DbContext. It has three DbSet properties, one for each of my three POCO classes. In OnModelCreating I’m telling the ModelBuilder about my ComplexType; the wrapper for my enum MessageStatus. This doesn’t happen automatically, see here and here. Lastly, here is another good post by ScottGu, which will allow you to tweak your DB schema mapping (also by overriding OnModelCreating). GLHF!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>