Entity Framework Code first 禁止重新生成數(shù)據(jù)表庫數(shù)據(jù) 和實體類修改后如何對應(yīng)表
嘗試使用 Entity Framework Code first 寫好代碼后 數(shù)據(jù)庫自動生成 很簡單方便 。 現(xiàn)在問題來了 需求變化 需要增加字段 甚至增加表
這時會報錯 增加字段保存出來的東西是 The model backing the 'BlogDB' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data. 增加表報錯的是 更新條目錯誤 原因是EdmMetadata 有個這個表 再檢查與實體是否匹配 只要把這個表刪了 然后手動增加字段 讓字段與實體匹配 就可以了 但不知道這樣會有什么不好的影響沒~
這里再說下另一種方法 閱讀上面的報錯信息 其中有這么一句or call Database.SetInitializer with an
IDatabaseInitializer instance. 調(diào)用Database.SetInitializer方法來自動重建數(shù)據(jù)庫 但是這就導(dǎo)致了數(shù)據(jù)會丟失的問題 不過開發(fā)初期還是可以用的
這篇文章有講解
浙公網(wǎng)安備 33010602011771號