The model backing the 'ApplicationDbContext' context has changed since the database was created

前端 未结 21 1360
情话喂你
情话喂你 2020-12-02 07:24

First of all, I have not seen this error anywhere else and I guess it\'s not a replicate so please read the whole situation first.

Everything was working just fine

相关标签:
21条回答
  • 2020-12-02 07:40

    simply the error means that your models has changes and is not in Sync with DB ,so go to package manager console , add-migration foo2 this will give a hint of what is causing the issue , may be you removed something or in my case I remove a data annotation . from there you can get the change and hopefully reverse it in your model.

    after that delete foo2 .

    0 讨论(0)
  • 2020-12-02 07:43

    This worked for me - no other changes required.

    DELETE FROM [dbo].[__MigrationHistory]
    
    0 讨论(0)
  • 2020-12-02 07:44

    When I am developing, I prefer to use this practical class to configure Migrations.

    Hope it helps.

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    
            Database.SetInitializer(new StackOverflowInitializer());
        }
    
        public class StackOverflowInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
        {
            public StackOverflowInitializer()
            {
                // TODO NOTHING, COMMENT ALL
    
                // IF CHANGES, RECREATE
                Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
    
                // CREATE ONLY NOT EXITS
                //Database.SetInitializer<Context>(new CreateDatabaseIfNotExists<ApplicationDbContext>());
            }
    
        }
    
        public System.Data.Entity.DbSet<stackoverflow.Models.Company> Companies { get; set; }
    
    }
    
    0 讨论(0)
  • 2020-12-02 07:45

    this comes because you add some property to one of your model and you did not update-Database . to solve this you have to remove it from model or you have to add-migration anyProperName with that properties and Update-database.

    0 讨论(0)
  • 2020-12-02 07:46

    I know I am very late, but I want to give contribution too. This error is really strange, because the browser is not able to understand how the changes should be rendered because the classes and their properties might have changed but not committed to the database.

    So do one thing,

    create one migration in Package Manager Console(Tools > NuGet Package Manager > Package Manager Console) using this command:

    add-migration UpdateMigration

    where UpdateMigration is the name of your Migration. You can give it any name of your choice but please be specific.

    After that, we just need to update the database, so run this:

    update-database

    Now that you have committed your changes to the database, just refresh your browser and there you go!

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 07:47

    From the Tools menu, click NuGet Package Manger, then click Package Manager Console (PMC). Enter the following commands in the PMC.

    Enable-Migrations Add-Migration Init Update-Database Run the application. The solution to the problem is from here

    0 讨论(0)
提交回复
热议问题