我开始学习实体框架所以裸跟我这的 noobish 的问题。
I'm starting to learn Entity Framework so bare with me with this noobish question.
我有一个不断显示每次我运行应用程序此错误消息。我使用的是实体框架5:$ C C首先$
I have this error message that keeps on displaying everytime I run the application. I'm using Entity Framework 5: Code First
下面是错误消息,
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:MyDocsVisual Studio 2012ProjectsLaundryService_DEMOLaundryService_DEMOfrmMain.cs:line 39
当我创建了一个叫做发票实体此错误的开始。下面是实体的全code,
this error started when I created an entity called invoice. Here's the full code of the entity,
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);
}
}
为了复制应用程序,我已经包括这是可供下载 。所以这个问题不会满code。
In order to replicate the application, i have included the project file which is available for download. And so this question will not be full of code.
如果有,我已经错过了在这个问题的详细信息,请告诉这样我就可以包含它。
If there are details that I've missed in the question, please tell so I can include it.
谢谢!
我发现code,通过改变工作
I found the code working by changing
static LaundryShopContext()
{
Database.SetInitializer<LaundryShopContext>(
new DropCreateDatabaseIfModelChanges<LaundryShopContext>());
}
到
static LaundryShopContext()
{
Database.SetInitializer<LaundryShopContext>(
new DropCreateDatabaseAlways<LaundryShopContext>());
}