ASP.NET MVC 3 app demonstrating code first approach enabled by Entity Framework 4 CTP4 

Friday, October 22, 2010 5:14 PM

Some developers like the designers and would want to develop using the designer approach, while some others like coding their classes maintaining full control over all the code. Entity Framework 4 CTP4 now caters to both the styles equally well.

In this sample we will see how to write an ASP.NET MVC 3 application starting with a class to represent our data model, controllers and views to provide the UI and finally storing the data in a database. We will use SQL CE 4 in this instance. We will also need the Entity Framework 4 CTP4.

The steps really are very simple:

  1. Start with a new ASP.NET MVC 3 Web Application with Razor View Engine.
  2. Create a Customer class in the Model folder
  3. Build the project so that the Customer class is available in intellisense
  4. Create a CustomersController in the Controllers folder. Make sure you place a check mark against ‘ Add action methods….’
  5. Modify the Create action method to instantiate a Customer object and pass it to the View.
  6. Right click and add a strongly typed view Create and make sure that the View Content is set to Create.
  7. Execute the application and navigate to Customers/Create within the website.
  8. Modify the postback handler for Create to redirect to ‘/’ so that the app does not complain about the ‘Index’ view (we have not yet coded the Index view).
  9. Add data annotations to the Customer class for Validations and add Validation logic in postback handler for Create

Now we have a functioning application with the UI as we expect, but the data is not being stored in a data base. Now to store the data in a SQL CE 4 database by name CRM, we need to do the following:

  1. Add a class file CRM within the Model folder
  2. Add a reference to Entity Framework 4 CTP4 (Microsoft.Data.Entity.Ctp which is installed in \Program Files (x86)\Microsoft ADO.NET Entity Framework Feature CTP4\Binaries folder) and to System.Data.Entity
  3. Derive the CRM class from DbContext
  4. Add the code ‘public DbSet<Customer> Customers { get;  set; }’ to the CRM class
  5. Add the following Connection String Configuration to the Web.Config file

                <add name=”CRM”
                           connectionString=”Data Source=|DataDirectory|CRM.sdf”
                           providerName=”System.Data.SqlServerCe.4.0” />

  6. Add the App_Data folder to the project
  7. Instantiate the CRM class in the CustomersController

    CRM crm = new CRM();
  8. Add the following 2 lines of code in the postback handler for create right after the validation to save the new Customer


  9. Build & Execute the application. EF will figure out that the CRM.sdf does not exist in your App_Data folder and will create it. It will also infer the schema from the Models defined in the classes derived from DbContext.

If you want to verify that the data is indeed being saved to the data base, implement the Index view as follows:

  1. Replace the code in the Index Action on the Controller with:

    var customers = from c in crm.Customers
                    select c;
    return View(customers.ToList());


  2. Right click within the Index Action and Add View for Index. Make sure that you select ‘List’ for View Content.
  3. Build & Execute the application. The Create New link will work and allow you to add new Customers. The Edit, Details and Delete links will not work as we have not yet implemented the code for them.

Here is a video that demonstrates all of this functionality. Hope this is helpful.

Share This Using Popular Bookmarking Services

re: ASP.NET MVC 3 app demonstrating code first approach enabled by Entity Framework 4 CTP4

Wednesday, November 17, 2010 10:24 AM Deepak Chitnis

@Misu Thanks.

re: ASP.NET MVC 3 app demonstrating code first approach enabled by Entity Framework 4 CTP4

Wednesday, November 17, 2010 3:18 AM Misu

Very good presentation. Thanks.

Comments are closed on this post.