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

前端 未结 21 1494
情话喂你
情话喂你 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:44

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

    Hope it helps.

    public class ApplicationDbContext : IdentityDbContext
    {
        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();
            modelBuilder.Conventions.Remove();
    
            Database.SetInitializer(new StackOverflowInitializer());
        }
    
        public class StackOverflowInitializer : DropCreateDatabaseIfModelChanges
        {
            public StackOverflowInitializer()
            {
                // TODO NOTHING, COMMENT ALL
    
                // IF CHANGES, RECREATE
                Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
    
                // CREATE ONLY NOT EXITS
                //Database.SetInitializer(new CreateDatabaseIfNotExists());
            }
    
        }
    
        public System.Data.Entity.DbSet Companies { get; set; }
    
    }
    

提交回复
热议问题