Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 2.15 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Entity Framework 4.1, Table Per Type Inheritance
  2. public abstract class Person
  3.   {
  4.  
  5.     public int ID { get; set; }
  6.  
  7.     public string Name { get; set; }
  8.  
  9.     public string Email { get; set; }
  10.   }
  11.  
  12.   public class Employee : Person
  13.   {
  14.  
  15.     public string Number { get; set; }
  16.  
  17.     public System.DateTime HireDate { get; set; }
  18.   }
  19.  
  20.   public class User : Person
  21.   {
  22.  
  23.     public string Password { get; set; }
  24.  
  25.     public bool Active { get; set; }
  26.   }
  27.  
  28.   public class CfTestContext : DbContext
  29.   {
  30.  
  31.     public DbSet<Employee> Employees { get; set; }
  32.  
  33.     public DbSet<User> Users { get; set; }
  34.  
  35.     public DbSet<Person> People { get; set; }
  36.  
  37.  
  38.     protected override void OnModelCreating(DbModelBuilder modelBuilder)
  39.     {
  40.  
  41.       modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
  42.  
  43.       modelBuilder.Entity<Person>().ToTable("People");
  44.  
  45.       modelBuilder.Entity<Employee>().ToTable("Employees");
  46.  
  47.       modelBuilder.Entity<User>().ToTable("Users");
  48.     }
  49.   }
  50.  
  51.   public class CreateCfTestDatabase : DropCreateDatabaseAlways<CfTestContext>
  52.   {
  53.  
  54.     protected override void Seed(CfTestContext context)
  55.     {
  56.  
  57.       // Abraham Lincoln hired as an Employee on February 12th.
  58.  
  59.       // Abraham doesn't need access as a User because he doesn't use the Purchasing application.
  60.  
  61.       context.Employees.Add(new Employee { Name = "Abraham Lincoln", Email = "abraham@microsoft.com", Number = "107124", HireDate = System.DateTime.Parse("2/12/2000") });
  62.  
  63.       // George Washington added as a User on July 4th.
  64.  
  65.       // George is a consultant, so he will never become an Employee.
  66.  
  67.       context.Users.Add(new User { Name = "George Washington", Email = "george@microsoft.com", Password = "xT76#a2", Active = true });
  68.  
  69.       context.SaveChanges();
  70.  
  71.       // Make Abraham Lincoln a User on September 29th.
  72.  
  73.       // Abraham received training and now needs to use the Purchasing application
  74.  
  75.       Employee employee = context.Employees.Where(t => t.Name == "Abraham Lincoln").FirstOrDefault();
  76.  
  77.       context.Users.Add(new User { Password = "C1x$av38", Active = true,  ID = employee.ID });  // this does not produce the desired results.
  78.  
  79.       context.SaveChanges();
  80.  
  81.       base.Seed(context);
  82.     }
  83.   }