0

I'm trying to add a new sub-entity, product component ProductRevComp to an existing entity ProductRev. However when I retrieve an instance of the ProductRev class, the Comps collection is never populated (even when explicitly Including() it). I BELIEVE I have mapped everything correctly, but it has taken more fiddling than I want and this is the most likely place for a mistake to be hiding. However profiling the SQL statements show the relevent columns are being populated with the correct data.

Checking db.ProductRevComps (i.e. the DbSet of all my comps) shows the records can be loaded, and that mapping is working as expected.

Mappings:

    public class ProductRevConfiguration : EntityTypeConfiguration<ProductRev>
    {
        public ProductRevConfiguration()
        {
            HasKey(p => p.ProductRevId);
            HasMany(p => p.Comps).WithRequired().HasForeignKey(p => p.ParentProductRevId);
            Ignore(p => p.ProgrammedParts);
        }
    }

    public class ProductRevCompConfiguration : EntityTypeConfiguration<ProductRevComp>
    {
        public ProductRevCompConfiguration()
        {
            HasKey(p => new { p.ParentProductRevId, p.CompProductRevId });
            HasRequired(p => p.ParentProductRev).WithMany().HasForeignKey(p => p.ParentProductRevId);
            HasRequired(p => p.CompProductRev).WithMany().HasForeignKey(p => p.CompProductRevId);
        }
    }

Product entity (amazingly simplified):

public class ProductRev
{
    public string ProductRevId { get; set; }
    public virtual List<ProductRevComp> Comps { get; set; }
    public virtual List<ProductRevComp> ProgrammedParts { get { return Comps; } }//Will be filtered once I get this working

    public ProductRev() { }
}    

Comp entity:

public class ProductRevComp
{
    public string ParentProductRevId { get; set; }
    public virtual ProductRev ParentProductRev { get; set; }

    public string CompProductRevId { get; set; }
    public virtual ProductRev CompProductRev { get; set; }

    public int CompTypeValue { get; set; }
    public ProductRevCompType CompType
    {
        get { return (ProductRevCompType)CompTypeValue; }
        set { CompTypeValue = (int)value; }
    }

    public enum ProductRevCompType { ProgrammedPart = 1 };

    public ProductRevComp() { }

    public override string ToString()
    {
        return base.ToString();
    }

}

Removing the extra prog parts collection doesn't change anything.

How can I get the ProductRev entity to populate the Comps property without resorting to a manual DB hit?

(Must run as the office is closing and I don't have a key - I hope I have included all details, please comment if anything is missing.)

Visser
  • 188
  • 1
  • 10
  • You need to make your `ProductRev` property `virtual` to enable lazy/eager loading, that will fix your problem – Alexander Derck Feb 02 '16 at 07:42
  • D'oh. That's definitely something I missed that I normally do (and a very silly to boot) - will try ASAP tomorrow. But it was still definitely attempting to map those properties without the `virtual` - I had several issues earlier caused by these mappings, sans `virtual`. So (just out of curiosity) why is the virtual required if its still mapped without it assuming I don't care if it is lazy loaded? – Visser Feb 02 '16 at 07:57
  • Unfortunately this did not fix the problem. – Visser Feb 02 '16 at 22:47

0 Answers0