Created
November 7, 2013 03:09
-
-
Save gabrieljoelc/7348340 to your computer and use it in GitHub Desktop.
Table per Type Entity Framework code first inheritance strategy. From http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BillingDetail | |
{ | |
public int BillingDetailId { get; set; } | |
public string Owner { get; set; } | |
public string Number { get; set; } | |
} | |
[Table("BankAccounts")] | |
public class BankAccount : BillingDetail | |
{ | |
public string BankName { get; set; } | |
public string Swift { get; set; } | |
} | |
[Table("CreditCards")] | |
public class CreditCard : BillingDetail | |
{ | |
public int CardType { get; set; } | |
public string ExpiryMonth { get; set; } | |
public string ExpiryYear { get; set; } | |
} | |
public class InheritanceMappingContext : DbContext | |
{ | |
public DbSet<BillingDetail> BillingDetails { get; set; } | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
modelBuilder.Entity<BankAccount>().ToTable("BankAccounts"); | |
modelBuilder.Entity<CreditCard>().ToTable("CreditCards"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment