Enable Automatic Migrations in mvc

15,018

Solution 1

For automatic migrations first you should set Database Initializer in your Database Context constructor.

public class YourDBContext: DbContext 
{        
    public YourDBContext(): base("ConnectionString") 
    {
         Database.SetInitializer<YourDBContext>(new CreateDatabaseIfNotExists<YourDBContext>());
    }

You can also create custom Database initializer implementing CreateDatabaseIfNotExists<YourDBContext> with overridden seed method to provide initial data.

Second you should enable automatic migrations using command in Package Manager Console Enable-Migrations -EnableAutomaticMigrations

Then using Update-Database command in Package Manager Console will apply to database all the pending changes that you have made after last migration.

Update : Use Verbose with update database command to see what script is generated

Update-Database –Verbose

Solution 2

You should use DropCreateDatabaseIfModelChanges if you always want to recreate the db rather than targeting the initialdatabase migration. More on the topic on the asp.net website In your context's constructor, e.g

public class SchoolDBContext: DbContext 
    {        
        public SchoolDBContext(): base("SchoolDBConnectionString") 
        {
         Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>());
        }

Solution 3

If you want general help, I think Microsoft's documentation is fairly good for starters. They also have some good tutorials to get you started correctly from the beginning.

For your current issue, could you elaborate on the exact issue? The error message shows something you'll need to fix yourself and is not necessarily an issue with EF.

Update:

Modify the custom DbContext's constructor with following line:

Database.SetInitializer<MyDbContext>(null);
Share:
15,018
Vini
Author by

Vini

I started using SO in 2014 during my first development job in C#. Ever since I have been reading and learning a lot from SO. I am person with great hopes in Entrepreneurship but not forgetting the development skills that I have learned so far and still learning. I always am open to opportunities to meet people and learn new things. I always love the moment when I can think, "I would have never thought about it this way". Technology and people never cease to amaze me.

Updated on June 05, 2022

Comments

  • Vini
    Vini over 1 year

    I am creating a MVC application where I add new classes very often. As the classes are related to each other and the test data in the the tables already created are of no interest I prefer to go with AutomaticMigrations.

    i use the following command to set the database to the initial state.

    Update-Database –TargetMigration: $InitialDatabase
    

    But the problem is that it doesn't work always. What could be the possible reasons that it goes wrong? I am not familiar with Code First Migrations.

    The error is mostly thrown during the runtime stating the DbContext was changed. I make sure there are no dependencies in the existing tables with the dropped tables before enabling the Migrations. I also get an error that states

    Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
    

    I am looking for general help on the topic.