Skip to content

Instantly share code, notes, and snippets.

@milutinovici
Last active December 15, 2015 21:29
Show Gist options
  • Save milutinovici/5325614 to your computer and use it in GitHub Desktop.
Save milutinovici/5325614 to your computer and use it in GitHub Desktop.
Almost generic entities, almost working. Include is being ignored. Tested on EF6 alpha3.
public class Product
{
public int Id { get; set; }
public string Manufacturer { get; set; }
public string Name { get; set; }
}
public class Camera : Product
{
public double Megapixels { get; set; }
}
public class Offer<TProduct> where TProduct : Product
{
[Key, Column(Order = 0)]
public int SellerId { get; set; }
public User Seller { get; set; }
[Key, Column(Order = 1)]
public int ProductId { get; set; }
public TProduct Product { get; set; }
public decimal Price { get; set; }
}
public class CameraOffer : Offer<Camera> { }
public class MyContext : DbContext
{
public DbSet<CameraOffer> CameraOffers { get; set; }
public DbSet<PhoneOffer> PhoneOffers { get; set; }
public IQueryable<Offer<TProduct>> OffersFor<TProduct>() where TProduct: Product
{
var type = Assembly.GetCallingAssembly().ExportedTypes.First(x => x.IsSubclassOf(typeof(Offer<>).MakeGenericType(typeof(TProduct))));
var set = this.Set(type) as IQueryable<Offer<TProduct>>;
return set.Include(x => x.Product);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment