Ciwan Ciwan - 1 year ago 74
C# Question

Include list of objects on abstract parent EF Code First

I have an

class named
that looks like this:

public abstract class Business
public int Id { get; set; }
public string Name { get; set; }
public string TaxNumber { get; set; }
public string Description { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public string Email { get; set; }
public bool IsDeleted { get; set; }
public virtual IEnumerable<Address> Addresses { get; set; }
public virtual IEnumerable<Contact> Contacts { get; set; }

One of the classes that inherits from the above is the
class. That looks like this:

public class Supplier : Business
public virtual ICollection<PurchaseOrder> PurchaseOrders { get; set; }

All is good, but when I come to grab a supplier for my MVC front-end, I would like to include the
associated with the Supplier.

I tried this:

public Supplier GetSupplier(int id)
return _context.Businesses.Include(b => b.Addresses).OfType<Supplier>().SingleOrDefault(x => x.Id == id);

But it doesn't work, it says there is no
property on

Any help would be greatly appreciated.

Thank you.

Answer Source

If you change your collection from IEnumerable to ICollection your code should work fine

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download