0
votes

Entity Framework 6. Get include property throws error does not declare a navigation property I have to entities:

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

In context I do:

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

In DataBase result is:

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

If i run:

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

Works fine

But If I run:

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

I get an error

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'.

I cant't understand what could be wrong...

1
Can you please add the registration code of the Product DbSet (i.e. public DbSet<Product> Products { get; set; })? In the code snippet that worked you access context.rowDocSale... and not context.RowDocSale. Could it be that the DbSet properties on DataModelsDbContext are declared as rowDocSale and products? If yes, try accessing context.products. - rufer7
Edited. I can access to products. But if i use Include(p => p.VatRate) with products, comes error. In case I use Include(p => p.VatRate) with RowDocumentSale, everything Ok - dm k
Ok, thank you. I cannot see any problem in the configuration... I will have another look onto your problem as soon as I have time. - rufer7
change your entity's property name and than try i'm not sure but it might be the problem public virtual VatRate VatRates { get; set; } - Curiousdev
Do you have a primary key defined in your Product and RowDocumentSale classes? Something like: [Key] public int Id { get; set; } - rufer7

1 Answers

0
votes

Solved: I Don't know why, but this helped: it was:

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

Now is:

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