Database Reference
In-Depth Information
Start by adding a console application project to Visual Studio entitled Recipe10 . Be certain to reference 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. Doing so will
download, install, and configure the Entity Framework 6 libraries in your project.
Next we create three entity objects. Create two classes: Order and OrderItem , and copy the code from Listing 5-23
into the classes.
Listing 5-23. Entity Classes
public class Order
{
public Order()
{
OrderItems = new HashSet<OrderItem>();
}
public int OrderId { get; set; }
public System.DateTime OrderDate { get; set; }
public string CustomerName { get; set; }
public virtual ICollection<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public int OrderItemId { get; set; }
public int OrderId { get; set; }
public int SKU { get; set; }
public int Shipped { get; set; }
public decimal UnitPrice { get; set; }
public virtual Order Order { get; set; }
}
Next create a class entitled Recipe10Context and add the code from Listing 5-24 to it, ensuring the class derives
from the Entity Framework DbContext class.
Listing 5-24. Context Class
public class Recipe10Context : DbContext
{
public Recipe10Context()
: base("Recipe10ConnectionString")
{
// Disable Entity Framework Model Compatibility
Database.SetInitializer<Recipe10Context>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().ToTable("Chapter5.Order");
modelBuilder.Entity<OrderItem>().ToTable("Chapter5.OrderItem");
}
 
Search WWH ::




Custom Search