Database Reference
In-Depth Information
public class Phone : BaseEntity
{
public int PhoneId { get; set; }
public string Number { get; set; }
public string PhoneType { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
5.
Add a reference in the Recipe4.Service project to the Entity Framework 6 libraries.
Leveraging the NuGet Package Manager does this best. Right-click on Reference,
and select Manage NuGet Packages. From the Online tab, locate and install the Entity
Framework 6 package.
6.
Then add a new class entitled Recipe4Context , and add the code from Listing 9-21 to it,
ensuring that the class derives from the Entity Framework DbContext class. Note closely
how we leverage a new Entity Framework 6 features entitled “ Configuring Unmapped
Base Types .” In Listing 9-21, we define a convention that instructs each entity class to
“ignore” (i.e., not map to the underlying database) the TrackingState property from the
BaseEntity base class which we include only to track the state of the disconnected entities
in operations that cross service boundaries.
rowan martin, microsoft program manager for the entity Framework team has published a helpful blog
post about Configuring Unmapped Base types: http://romiller.com/2013/01/29/ef6-code-first-configuring-
unmapped-base-types/ . Be certain to check out rowan's other outstanding blog posts on the entity Framework.
Note
Listing 9-21. Context Class
public class Recipe4Context : DbContext
{
public Recipe4Context() : base("Recipe4ConnectionString") { }
public DbSet<Customer> Customers { get; set; }
public DbSet<Phone> Phones { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Do not persist TrackingState property to data store
// This property is used internally to track state of
// disconnected entities across service boundaries.
// Leverage the Custom Code First Conventions features from Entity Framework 6.
// Define a convention that performs a configuration for every entity
// that derives from a base entity class.
modelBuilder.Types<BaseEntity>().Configure(x => x.Ignore(y => y.TrackingState));
modelBuilder.Entity<Customer>().ToTable("Chapter9.Customer");
modelBuilder.Entity<Phone>().ToTable("Chapter9.Phone");
}
}
 
 
Search WWH ::




Custom Search