0

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

dm k
  • 111
  • 3
  • 9
  • Can you please add the registration code of the Product DbSet (i.e. `public DbSet 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 Mar 16 '17 at 09:01
  • 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 Mar 16 '17 at 09:16
  • 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 Mar 16 '17 at 09:22
  • 1
    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 Mar 16 '17 at 10:04
  • Do you have a primary key defined in your Product and RowDocumentSale classes? Something like: [Key] public int Id { get; set; } – rufer7 Mar 16 '17 at 10:22
  • Do you use code first approach? – rufer7 Mar 16 '17 at 10:41
  • Yes. Code First. I edited post. So key is defined – dm k Mar 16 '17 at 11:03
  • What properties does `Product` inherit? – rufer7 Mar 16 '17 at 11:15
  • Both classes are big with a lot of properties with 3 levels of inheritance. I wrote here just problem properties. – dm k Mar 16 '17 at 11:58
  • @dmk I tried to reproduce the problem, but did not succeed as in my project with the same entities it worked fine. Are you sure there are no pending migrations? – rufer7 Mar 16 '17 at 16:05
  • :( No any pending transactions... – dm k Mar 17 '17 at 06:22
  • If id Product pr = context.Products.FirstOrDefault(); VatRate v = pr.VatRate; Works fine but for 2 query – dm k Mar 17 '17 at 09:10

1 Answers1

0

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; }
}
dm k
  • 111
  • 3
  • 9