EF code first - Model compatibility cannot be checked because the database does not contain model metadata

前端 未结 5 922
一向
一向 2020-12-29 16:16

I have enabled automatic migrations. Then, I deleted my whole db. Next, i executed Update-database from command console, and it recreated my db. Then, I started

5条回答
  •  余生分开走
    2020-12-29 16:35

    I use Entity Framework 6 , SQL 2008 R2 , VS 2013.
    To solve this problem only use the following procedure :

    1) delete existing db ( existing database that created with EF model{code first})
    2) Run APP again.

    Fore example query code (in Layout):
    this code create db if my model is change and search username in user table.

    
        @{
            // Delete && Create ...
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
    
            var db = new DBContext();
            var SearchingUser = db.Users.Where(c => c.UserName == "qwertyui");
            if (SearchingUser.Count() == 0) {
                var User = new Users { UserName = "qwertyui",Password = "12345678" };
                db.Users.Add(User);
                db.SaveChanges();
            }
    
        }
    
    
        @RenderSection("scripts", required: false)
        @RenderBody()
    
    

提交回复
热议问题