EF5 获取到以下错误信息:由于数据库中不包含模型元数据,因此无法检查模型的兼容性。
EF5 获取到以下错误信息:由于数据库中不包含模型元数据,因此无法检查模型的兼容性。
每次运行应用程序时都会显示这个错误消息。我正在使用Entity Framework 5:Code First。
以下是错误消息:
System.NotSupportedException: 由于数据库不包含模型元数据,无法检查模型兼容性。只能对使用Code First或Code First Migrations创建的数据库进行模型兼容性检查。
在System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata)
在System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
在System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
在System.Data.Entity.DropCreateDatabaseIfModelChanges`1.InitializeDatabase(TContext context)
在System.Data.Entity.Database.<>c__DisplayClass2`1.
在System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.
在System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
在System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
在System.Data.Entity.Internal.LazyInternalContext.
在System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
在System.Data.Entity.Internal.InternalContext.Initialize()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
在System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
在System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
在System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
在System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
在System.Data.Entity.DbSet`1.Add(TEntity entity)
在LaundryService_DEMO.frmMain.btnCreate_Click(Object sender, EventArgs e) in d:\MyDocs\Visual Studio 2012\Projects\LaundryService_DEMO\LaundryService_DEMO\frmMain.cs:line 39
这个错误是在我创建一个名为invoice的实体时出现的。以下是实体的完整代码:
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
{
public InvoiceMap()
{
// 主键
this.HasKey(x => x.InvoiceID);
// 属性和映射
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");
// 关系
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);
}
}
为了复制该应用程序,我已经包含了项目文件,可以通过下载。如果问题中有我遗漏的细节,请留言告诉我。