2013-03-13 54 views
4

我已启用自动迁移。然后,我删除了我的整个数据库。接下来,我从命令控制台执行Update-database,并重新创建了我的db。于是,我开始只是我的应用程序,看看这个错误:EF代码优先 - 模型兼容性无法检查,因为数据库不包含模型元数据

Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.

那么究竟什么是元数据,我怎么可以指向实体框架呢?

PS。我的数据库包含名为MigrationsHistory的表。

回答

6

这里是可能的方法的详细说明,以解决这里面我写了前一阵子...
(不是您遇到什么,因此没有重复本身,而是在考虑不同的情景)

https://stackoverflow.com/a/10255051/417747

总结...

What works for me is to use Update-Database -Script

That creates a script with a 'migration difference', which you can manually apply as an SQL script on the target server database (and you should get the right migration table rows inserted etc.).

If that still doesn't work - you can still do two things...

a) remove the migration table (target - under system tables) - as per http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx comments in there - that should fail back to previous behavior and if you're certain that your Db-s are the same - it's just going to 'trust you',

b) as a last resort I used - make a Update-Database -Script of the full schema (e.g. by initializing an empty db which should force a 'full script'), find the INSERT INTO [__MigrationHistory] records, just run those, insert them into the database, and make sure that your databases - and code match,

that should make things run in sync again.

如果它可以帮助

+0

https://blogs.msdn.microsoft.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough。aspx – Kiquenet 2017-06-03 12:20:30

+0

@Kiquenet里面没有多少东西,让我知道你需要什么 - 它是你需要移除的__MigrationHistory表 - 并且据我所知没有任何其他相关的东西(只有一张桌子,在系统表下)。注释是沿着这些线,如果EF没有找到该表,它只是回落到'信任'你,即你的数据库模式和代码是同步的。所以如果你知道这些是同步的,你总是可以删除那张表(或者我建议在(b)下做)。我在上面(a)中重新评论了这一评论。所以没有丢失:) – NSGaga 2017-06-09 09:32:40

+0

页面可以在这里找到[存档](http://web.archive.org/web/20160718142810/http://blogs.msdn.com/b/adonet/archive/2012/02/09 /ef-4-3-automatic-migrations-walkthrough.aspx),但实际上评论部分丢失了(有一些讨论 - 根据我上面所述) – NSGaga 2017-06-09 09:35:18

1

添加到您的背景:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 
} 
+0

什么是“IncludeMetadataConvention”?未找到 – 2014-05-23 18:39:28

0

我使用实体框架6,SQL 2008 R2,2013年VS
为了解决这个问题,只能使用以下步骤:

1)删除现有的DB(现有数据库用EF模型创建{code first})
2)再次运行APP。

前面的示例查询代码(在布局中):
此代码创建数据库,如果我的模型更改并在用户表中搜索用户名。

<body> 
    @{ 
     // Delete && Create ... 
     Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContext>()); 

     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() 
</body> 
1

拆下本地数据库例如说从Visual Studio的服务器资源管理器“,然后打开SQL Server Management Studio中“database1.mdf”右击数据库>附加,然后浏览相同的“database1.mdf”文件。如果您没有访问权限,请将&复制到m盘和ldf文件中,然后进行附加。

然后在sql server上打开新的查询窗口,然后像下面的查询一样复制您的身份表。

* 'SELECT * FROM Database1.dbo .__ MigrationHistory成[__MigrationHistory]' *

0
  1. 包管理控制台>启用-迁移-EnableAutomaticMigrations
  2. 配置迁移/ Configuration.cs
  3. 包Manager Console>更新 - 数据库