Database Reference
In-Depth Information
public int ProjectID { get; set; }
public string Name { get; set; }
public int ManagerID { get; set; }
public virtual ICollection<Contractor> Contractors { get; set; }
public virtual Manager Manager { get; set; }
}
Next create a class entitled Recipe11Context , and add the code from Listing 5-28 to it, ensuring the class
derives from the Entity Framework DbContext class.
Listing 5-28. Context Class
public class Recipe11Context : DbContext
{
public Recipe11Context()
: base("Recipe11ConnectionString")
{
// Disable Entity Framework Model Compatibility
Database.SetInitializer<Recipe11Context>(null);
}
public DbSet<Contractor> Contractors { get; set; }
public DbSet<Manager> Managers { get; set; }
public DbSet<Project> Projects { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Contractor>().ToTable("Chapter5.Contractor");
modelBuilder.Entity<Manager>().ToTable("Chapter5.Manager");
modelBuilder.Entity<Project>().ToTable("Chapter5.Project");
// Explilcitly map key for Contractor entity
modelBuilder.Entity<Contractor>().HasKey(x => x.ContracterID);
}
}
Next add an App.Config class to the project, and add the code from Listing 5-29c to it under the
ConnectionStrings section.
Listing 5-29. Connection String
<connectionStrings>
<add name="Recipe11ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Search WWH ::




Custom Search