2013-03-13 70 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>更新 - 數據庫
相關問題