Skip to content

Instantly share code, notes, and snippets.

@fenix2222
Created April 23, 2016 01:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
using System.Data.Entity;
using MySample.Entities;
namespace MySample.Data
{
public class MySampleDbContext : DbContext
{
public IDbSet<Product> Products { get; set; }
public IDbSet<ProductCategory> ProductCategories { get; set; }
public IDbSet<Order> Orders { get; set; }
public IDbSet<OrderLineItem> OrderLineItems { get; set; }
public MySampleDbContext()
: base("name=MySampleDbConnection")
{
this.Configuration.LazyLoadingEnabled = true;
}
public MySampleDbContext(string connectionString)
: base(connectionString)
{
this.Configuration.LazyLoadingEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().HasKey(o => o.Id);
modelBuilder.Entity<Order>().HasMany(o => o.LineItems).WithRequired().WillCascadeOnDelete(true);
modelBuilder.Entity<OrderLineItem>().HasKey(o => o.Id);
modelBuilder.Entity<OrderLineItem>().HasRequired(o => o.Product).WithMany().WillCascadeOnDelete(true);
modelBuilder.Entity<Product>().HasKey(o => o.Id);
modelBuilder.Entity<Product>().HasRequired(o => o.Category).WithMany().WillCascadeOnDelete(true);
modelBuilder.Entity<ProductCategory>().HasKey(o => o.Id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment