none
Entity Framework 6 Code First error: the model backing the context has changed since the database was created RRS feed

  • Question

  • I am using EF 6 code first approach. Getting this error. But I didnt do any changes to do models neither the database.

    If I clear database from tables and delete all migrations and re-create migrations, project runs. After first run, I stop project and run again. In second run I am getting this error.

    How can I detect problem?

    I can support additional information if requested.

    Tuesday, September 29, 2015 5:50 AM

Answers

  • Hi hakdag,

    If you want to update something on your code or your business logic. You has to update your database with Migration.
    But, we can make it to automatically upgrade the database. We can use the MigrateDatabaseToLatestVersion database
    Initializer. We can update the Program.cs file, as shown below:

    class Program
        {
            static void Main(string[] args)
            {
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogdbContext, Configuration>());
    
                using (var db = new BlogdbContext())
                {
                    db.BlogSets.Add(new BlogSet { Name = "Another Blog " });
                    db.SaveChanges();
    
                    foreach (var blog in db.BlogSets)
                    {
                        Console.WriteLine(blog.Name);
                    }
                }
    
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(); 
            }
    }
    

    For more information, please refer to the following articles:
    1. MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration> Class
    https://msdn.microsoft.com/en-us/library/hh829293%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396
    Best Regards,

    Vince Li

    Wednesday, September 30, 2015 11:40 AM

All replies

  • I realized that this happens when I change something in code. Not in models, but in controllers or views, etc.
    Tuesday, September 29, 2015 6:06 AM
  • I realized that this happens when I change something in code. Not in models, but in controllers or views, etc.

    Changing like what?
    Tuesday, September 29, 2015 1:19 PM
  • I realized that this happens when I change something in code. Not in models, but in controllers or views, etc.


    Changing like what?
    Does it matter? I mean, why do I have to update database when I change something in html? or update my business logic? When I update my code, or develop something, I am getting this context changed error. 
    Tuesday, September 29, 2015 1:24 PM
  • Hi hakdag,

    If you want to update something on your code or your business logic. You has to update your database with Migration.
    But, we can make it to automatically upgrade the database. We can use the MigrateDatabaseToLatestVersion database
    Initializer. We can update the Program.cs file, as shown below:

    class Program
        {
            static void Main(string[] args)
            {
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogdbContext, Configuration>());
    
                using (var db = new BlogdbContext())
                {
                    db.BlogSets.Add(new BlogSet { Name = "Another Blog " });
                    db.SaveChanges();
    
                    foreach (var blog in db.BlogSets)
                    {
                        Console.WriteLine(blog.Name);
                    }
                }
    
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(); 
            }
    }
    

    For more information, please refer to the following articles:
    1. MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration> Class
    https://msdn.microsoft.com/en-us/library/hh829293%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396
    Best Regards,

    Vince Li

    Wednesday, September 30, 2015 11:40 AM