EF5获取此错误消息:由于数据库不包含模型元数据,因此无法检查模型兼容性 [英] EF5 Getting this error message: Model compatibility cannot be checked because the database does not contain model metadata

查看:93
本文介绍了EF5获取此错误消息:由于数据库不包含模型元数据,因此无法检查模型兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个错误消息,每次运行应用程序时都会继续显示。我正在使用实体框架5:代码优先

I have this error message that keeps on displaying every time 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:\MyDocs\Visual Studio 2012\Projects\LaundryService_DEMO\LaundryService_DEMO\frmMain.cs:line 39

当我创建时,此错误开始一个称为发票的实体。这是实体的完整代码,

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);
    }
}

为了复制应用程序,我已经包括项目文件是可供下载。所以这个问题不会是完整的代码。

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 comment so I can include it.

推荐答案

我通过更改

static LaundryShopContext()
{
  Database.SetInitializer<LaundryShopContext>(
    new DropCreateDatabaseIfModelChanges<LaundryShopContext>());
}

into

static LaundryShopContext()
{
  Database.SetInitializer<LaundryShopContext>(
    new DropCreateDatabaseAlways<LaundryShopContext>());
}

这篇关于EF5获取此错误消息:由于数据库不包含模型元数据,因此无法检查模型兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆