我有這個錯誤消息,每次運行應用程序時都會一直顯示。我使用的是實體Framework 5: Code First
EF5獲取此錯誤消息:模型兼容性無法檢查,因爲數據庫不包含模型元數據
這裏的錯誤信息,
System.NotSupportedException: 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.
at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata)
at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
at System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
at System.Data.Entity.DropCreateDatabaseIfModelChanges`1.InitializeDatabase(TContext context)
at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at LaundryService_DEMO.frmMain.btnCreate_Click(Object sender, EventArgs e) in d:\MyDocs\Visual Studio 2012\Projects\LaundryService_DEMO\LaundryService_DEMO\frmMain.cs:line 39
當我創建了一個叫做發票實體此錯誤開始。下面是實體的全部代碼,
public class Invoice
{
public string InvoiceID { get; set; }
public string CustomerID { get; set; }
public string UserID { get; set; }
public decimal TotalAmount { get; set; }
public DateTime TransactionDate { get; set; }
public DateTime PaymentDate { get; set; }
public Customer CustomerField { get; set; }
public SystemUser SystemUserField { get; set; }
}
public class InvoiceMap : EntityTypeConfiguration<Invoice>
{
public InvoiceMap()
{
// Primary Key
this.HasKey(x => x.InvoiceID);
// Property(ies) and Mapping(s)
this.ToTable("Invoice");
this.Property(x => x.InvoiceID)
.IsRequired()
.HasMaxLength(15)
.HasColumnName("InvoiceID")
.HasColumnType("nVarchar");
this.Property(x => x.CustomerID)
.IsRequired()
.HasMaxLength(15)
.HasColumnName("CustomerID")
.HasColumnType("nVarchar");
this.Property(x => x.UserID)
.IsRequired()
.HasMaxLength(15)
.HasColumnName("UserID")
.HasColumnType("nVarchar");
this.Property(x => x.TotalAmount)
.IsRequired()
.HasColumnName("TotalAmount")
.HasColumnType("decimal");
this.Property(x => x.TransactionDate)
.IsRequired()
.HasColumnName("TransactionDate")
.HasColumnType("datetime");
this.Property(x => x.PaymentDate)
.IsOptional()
.HasColumnName("PaymentDate")
.HasColumnType("datetime");
// Relationship
this.HasRequired(x => x.CustomerField)
.WithMany(x => x.InvoiceCollection)
.HasForeignKey(y => y.CustomerID);
this.HasRequired(x => x.SystemUserField)
.WithMany(x => x.InvoiceCollection)
.HasForeignKey(y => y.UserID);
}
}
爲了複製應用程序,我已經包括項目文件是available for download。所以這個問題不會充滿代碼。
如果有詳細資料,我錯過了問題,請評論,所以我可以包括它。
只需完成以下答案: 錯誤是因爲您在運行代碼後更改模型。你有兩種方法可以解決它: 1.你可以改變數據庫創建的邏輯,你在答案中是如何做到的; 2.或者你可以開始使用實體框架遷移,就像第二個答案; – MayogaX
謝謝......它的工作。但它應該爲'ifmodelchanges'工作,因爲它以前一直在工作。 – Sami
+1提示。但我認爲,我們隱藏了錯誤,而不是解決它。 * IfModelChanges *的問題是它依賴於* _MigrationHistory *表。我們通過**總是**躲避子彈創建數據庫,因此不需要歷史記錄。但是,一旦以* Always *運行,我認爲應該創建歷史表。它不在我的情況,雖然...我只看到表:* MSreplication_options *,* spt_fallback_db *,* spt_fallback_dev *,* spt_fallback_usg *和* spt_monitor *。 –