Some Useful Tips in Changing Code First Model After Scaffolding

Introduction

In this article I am describing the various ways to do the changes while scaffolding the Entity Framework Code First Models. Generally, after scaffolding the existing model in MVC 5 using Visual Studio 2013, you will get the Server Error "The model backing the DbContext has changed since the database was created" as shown below:

Server Error After Model Scaffolding

Here, you will learn to get rid of this error. Use the following procedure to create a sample application.

Note: Please create an MVC Web Application in Visual Studio 2013 before using the following procedure.

Step 1: Add a class named Employee into the Models folder and replace the boilerplate code with the following code:

using System.Data.Entity;

 

namespace WebApplication18.Models

{

    public class Employee

    {

        public int ID { get; set; }

        public string Name { get; set; }

        public string Company { get; set; }

    }

 

    public class EmployeeDBContext : DbContext

    {

        public DbSet<Employee> Employees { get; set; }

    }

}

Step 2: Add the connection string after the connection string that is present in the Web.Config file as shown below:

<add name="EmployeeDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Employee.mdf;Integrated Security=True"

      providerName="System.Data.SqlClient" />

Step 3: Please scaffold the Controller with the Entity Framework with Views and check out the controller in the browser.

Now, we need to modify our class . Replace the Employee class code with the following code:

public class Employee

{

    public int ID { get; set; }

    public string Name { get; set; }

    public string Company { get; set; }

    public string Gender { get; set; }

    public string EmailID { get; set; }

 

}

Re-scaffold the Controller and run the new scaffolded controller in the browser and you will get the server error message in the browser as shown in the previous image.

There are a couple of ways to get rid of this server error as in the following:

  • Code First Migration
  • Modify the Connection String

Code First Migration

To use the Code First Migration, use the following procedure.

Step 1: Open the Package Manager Console.

Package Manager Console

Step 2: Enter the following command in the Package Manager Console:

Enable-Migrations -ContextTypeName (Your Application Name).Models.EmployeeDBContext

Enable Migration in Package Manager Console

Step 3: Enter the following command in the Package Manager Console:

add-migration Initial

Add Migration in Package Manager Console

Step 4: Enter the following command in the Package Manager Console:

update-database

Update Database in Package Manager Console

That's it. Now you can run the application and open the Employee Controller in the browser. This time you will not get any server error. This is the very efficient approach, because you need to change the model that is required to modify. There is no need to modify the other database. You will get extra files in your application.

Modify the Connection String

To do the changes into the application, this approach is much better. You can just change the Initial Catalog and the AttachDbFileName values in the Web.Config file.

Change the Configuration file as shown in the following code:

<add name="EmployeeDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\MyEmployee.mdf;Integrated Security=True"

      providerName="System.Data.SqlClient" />

That's it. Now you can run the application and open the Employee Controller in the browser. This approach creates the new database because you changed the database name in the file. This approach is to be ignored, if the existing database is very large.

Summary

This article will help you to make the changes after scaffolding the Entity Framework Code First Model using one of two ways. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all