ASP.NET MVC3 app (part 1) – Entity Framework and code first

Written by  on June 9, 2011 

How often you need to have cascading choices on UI in order to make your application user friendly? In my experience, almost every modern application has some form of hierarchy, and that is where cascading dropdowns are used. But, I want to make this less repetitive and more elegant. So, let’s begin. In first part (this) I will create an simple web app using entity framework code-first and make simple model which will be used for creating this functionality. It is one speedy run-through of new features, without advanced topics, so mind some bad practices, this blog post is not about good programming, first part is for MVC3/.NET 4 beginners with experience with older versions of MVC and .NET.

It will be an mvc3 razor internet application:

image

With latest MVC tools update there are already almost all necessary NuGet packages installed in new project:

image

I will just update all of jQuery packages as they have updates at the moment, and add EntityFramework.SqlServerCompact (NuGet will add dependencies), so SQL server won’t be needed.

As this is demo app, I will put everything into single project. It will be an product catalogue.

This is data model:

image

Central object will be product model (I will use vehicles domain), which has type, version, trim level and manufacturer. Simple enough. Objects on diagram are simple POCO objects with collection properties marked as virtual, so entity framework can override them and inject DynamicProxy objects for lazy loading.

This will be my repository:

image

For this DbContext to work, I need to do one more thing – to put connection string into my web.config:

image

As I don’t want to create test data every time I change my model, I can use database initializer class to create test data (this is useful for unit testing):

image

And this is it. I now have database and data. Actually, I will have it when I start my application, if I add this to global.asax:

image

To test this, I will use controller autoscaffold feature of new MVC tools update:

image

This will autocreate controller and all views Smile. After this action, starting app and visiting http://localhost:57095/Manufacturer will give:

image

Note that only manufacturers for which I created Models are in database. This is because I only added Models, and EF added all related objects, and BMW was not among them.

Using auto scaffold I created controllers for all model objects in couple of minutes.

This is end of part 1, I now have application which will I use to create unobtrusive cascading dropdowns. In the next part I will make cascading dropdown loading using standard methods (jQuery and ajax).

For more info about EF Code First, visit

http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx

and

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

UPDATE: Added part 2

Category : ASP.NET MVCComputersjQueryProgramming

Tags :

Comments

7 Responses

  1. pthalacker says:

    This is great. I have been spinning my wheels about how to seed many-to-many data. I think your post has given me a way to figure it out.

  2. pthalacker says:

    @obrad I have familiarized myself with configuration mapping. My problem is creating the list items to populate the many-to-many. (I need to do the same thing to create fake data for testing.) I can create a categories for a book the way you created types and versions. The book has a property Categories that is an ICollection. When I try to create a list of books similar to the manner above, I get a null reference exception on the book.

    books.Add( new Book {Title=”Entity Framework”, Categories={catCSharp, catEF}};

    I don’t believe the configuration mapping is an issue yet because I never get any books created in the list

  3. pthalacker says:

    @obrad

    Got it.
    books.Add( new Book {Title=”Entity Framework”, Categories=new List(){catCSharp, catEF}};

    Just a small logic error. The seed method is now populating the many2many relationship correctly.
    Thank you for showing me the way. (No configuration code needed by the way)

  4. obrad says:

    I know that it looks great without configuration code, but you may want to think about using fluent configuration code instead of attributes. It is easier to adapt to changes in model and database with fluent configuration once your app is in production with real world data and you cannot recreate database anymore.

  5. Bryan Smith says:

    Hi, any news about when VS2012 will be available?

  6. obrad says:

    Hello,

    You probably meant VS11? Beta is available on http://www.microsoft.com/download/en/details.aspx?id=28975
    Microsoft has not announced official date on release version, but we have beta :)

Leave a Reply

%d bloggers like this: