Skip to content

Instantly share code, notes, and snippets.

@fenix2222
Created April 23, 2016 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fenix2222/f4f0b5010e9d61a847ce1f467d89be37 to your computer and use it in GitHub Desktop.
Save fenix2222/f4f0b5010e9d61a847ce1f467d89be37 to your computer and use it in GitHub Desktop.
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