Database Reference
In-Depth Information
Listing 2-8. Creating the Product POCO Entity
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int SKU { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageURL { get; set; }
}
Add an auto-property of type DbSet<Product> to your DbContext subclass.
3.
Override the OnModelCreating() method of DbContext with the code in Listing 2-9.
4.
Listing 2-9. Overriding OnModelCreating in the DbContext Subclass
public class EF6RecipesContext : DbContext
{
public DbSet<Product> Products { get; set; }
public ProductContext() : base("name=EF6CodeFirstRecipesContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Product>()
.Map(m =>
{
m.Properties(p => new {p.SKU, p.Description, p.Price});
m.ToTable("Product", "Chapter2");
})
.Map(m =>
{
m.Properties(p => new {p.SKU, p.ImageURL});
m.ToTable("ProductWebInfo", "Chapter2");
});
}
}
How It Works
It seems all too common in legacy systems to find “extra” information for each row in one table tucked away in
another table. Often this happens over time as a database evolves, and no one is willing to break existing code by
adding columns to some critical table. The answer is to “graft on” a new table to hold the additional columns.
By merging two or more tables into a single entity or, as it is usually perceived, splitting a single entity across two
or more tables, we can treat all of the parts as one logical entity. This process is often referred to as vertical splitting .
 
Search WWH ::




Custom Search