Entity Framework 6. get include 属性 throws error does not declare a navigation 属性

Entity Framework 6. Get include property throws error does not declare a navigation property

Entity Framework 6. get include 属性 throws error does not declare a navigation 属性 我要实体:

class Product
{
        public virtual int? vatrateID { get; set; }
        public virtual VatRate VatRate { get; set; }
}
class RowDocumentSale
{
        public virtual int? vatrateID { get; set; }
        public virtual VatRate VatRate { get; set; }
}

在上下文中我这样做:

public DbSet<Product> Products { get; set; }
public DbSet<RowDocumentSale> rowDocSale { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<RowDocumentSale>().HasOptional(o => o.VatRate).WithMany().HasForeignKey(k => k.vatrateID);
        modelBuilder.Entity<RowDocumentSale>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("RowDocumentSales");
        })
            .HasKey(k => k.id).Property(k => k.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    modelBuilder.Entity<Product>().HasOptional(o => o.VatRate).WithMany().HasForeignKey(k => k.vatrateID);
        modelBuilder.Entity<Product>()
            .HasKey(k => k.productID)
                            .Map(m =>
                            {
                                m.MapInheritedProperties();
                                m.ToTable("products");
                            })
            .Property(k => k.productID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}

在数据库中的结果是:

table RowDocumentSales
  "vatrateID" integer,
    CONSTRAINT "FK_dbo.RowDocumentSales_dbo.VatRates_vatrateID" FOREIGN KEY ("vatrateID")
      REFERENCES dbo."VatRates" (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,

table Product
  "vatrateID" integer,
    CONSTRAINT "FK_dbo.products_dbo.VatRates_vatrateID" FOREIGN KEY ("vatrateID")
      REFERENCES dbo."VatRates" (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION

如果我运行:

            using (var context = new DataModelsDbContext())
        {
            list = context.rowDocSale.Include(p => p.VatRate).Take(10).ToList();
        }
        Assert.AreEqual(1, list[0].VatRate.id);

工作正常

但是如果我运行:

    List<Product> list;
    using (var context = new DataModelsDbContext())
    {
        list = context.Products.Include(p => p.VatRate).Take(10).ToList();
    }
    Assert.AreEqual(1, list[0].VatRate.id);

我收到一个错误

Additional information: A specified Include path is not valid. The EntityType 'WebApplication1.Models.DataModels.Product' does not declare a navigation property with the name 'VatRate'.

我不明白哪里出了问题...

已解决: 我不知道为什么,但这有帮助: 它是:

public class Product : BaseModel, IExportable
{
    public virtual VatRate VatRate { get; set; }
}

现在是:

public class Product : BaseModel, IExportable
{
    public virtual VatRate VatRate_ { get; set; }
}