Ciwan Ciwan - 25 days ago 16
C# Question

Include list of objects on abstract parent EF Code First

I have an

abstract
class named
Business
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
Supplier
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
Addresses
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
Addresses
property on
Supplier
.

Any help would be greatly appreciated.

Thank you.

Answer

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

Comments