You can try to use EF Inheritance for your objects. It's also available in Code First - http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
There are 3 different approaches based on what you need:
- Table per Hierarchy - when all objects are related to the same table
- Table per Type - when all classes can have common data in shared table, and all specific fields is mapped to another table
- Table per concrete class - when all classes have own table. In that case common data structure will be duplicated in each table
You need to map your Dress, Shoes entities based on some data in OnModelCreating method. For example:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.Map<Dress>(m => m.Requires("ProductCategoryID").HasValue(1))
.Map<Shoes>(m => m.Requires("ProductCategoryID").HasValue(2));
}
In that case when you will load them from DBContext, it will create needed type automatically:
List<Product> products = dbContext.Products.ToList();
foreach(var product in products)
{
var sale = product.GiveSale();
if (product is Dress) Console.WriteLine("It's dress!");
if (product is Shoes) Console.WriteLine("It's a pair of shoes!");
}
and then you can update data, and save Dress\Shoes as usual Poco objects in Code first by calling SaveChanges