0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228

2 Answers2

2

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

George Vovos
  • 7,563
  • 2
  • 22
  • 45
0

You must implement an override since the base class is abstract.

From MSDN

"Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class"

Or alternatively remove the abstract keyword so that the base is used by default.

Another alternative, remove the base and use an Interface instead unless you have a specific reason for using abstract classes.

Ross Miller
  • 656
  • 3
  • 9
  • What are the implications on the database if I use an override? Right now the above is producing a single table with a `Discriminator` column. – J86 Nov 09 '16 at 15:34