How can I use TPT inheritance models when primary keys have different names?

Using Entity Framework 4.1 against an obsolete database, I cannot generate a working set of TPT inheritance models that are not multiple and use different names for a common primary key.

I use the Organization, Account, and Company database tables as shown below:

Organization OrganizationID (int PK) OrgName (varchar) Company CompanyID (int PK) CompanyNo (varchar) Account AccountID (int PK) AccountNo (varchar) 

Account.AccountID and Company.CompanyID have an FK restriction that the values ​​in these columns must also be contained in Organ.OrganizationID, so they cannot exist without the Organization row. If I designed these tables from scratch, then Account and Company would use OrganizationID as the primary key.

 public partial class BusinessEntities : DbContext { public BusinessEntities() : base("name=BusinessEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Organization>().ToTable("Organization"); // Override pluralization modelBuilder.Entity<Company>().ToTable("Company"); modelBuilder.Entity<Account>().ToTable("Account"); // Set primary key column for each modelBuilder.Entity<Company>().Property( e => e.OrganizationID).HasColumnName("CompanyID"); modelBuilder.Entity<Account>().Property( e => e.OrganizationID).HasColumnName("AccountID"); } public DbSet<Organization> Organization { get; set; } } public partial class Organization { public int OrganizationID { get; set; } public string OrgName { get; set; } } public partial class Account : Organization { public string AccountNo { get; set; } } public partial class Company : Organization { public string CompanyNo { get; set; } } 

When I try to use the simple selection code below, I get an error:

The 'OrganizationID' property is not a declared property of the type 'Company'. Ensure that the property has not been explicitly excluded from the model using the Ignore method or the NotMappedAttribute data annotations. Make sure this is a valid primitive property.

 static void Main(string[] args) { using (var context = new BusinessEntities()) { foreach (var b in context.Organization.OfType<Company>()) { Console.WriteLine("{0} {1}", b.CompanyNo, b.OrgName); } foreach (var b in context.Organization.OfType<Account>()) { Console.WriteLine("{0} {1}", b.AccountNo, b.OrgName); } } Console.ReadLine(); } 
+3
source share
1 answer

I have described TPT mapping here . This is step-by-step, so it should work, but you need to display identifiers in child entities.

But the problem is that you start with Model-first, generate a DdContext, which depends on EDMX, and then removes EDMX and starts to determine the mapping yourself. You must decide whether you want to use Model-first or Code-first first. You just wasted your efforts at EDMX.

If you want to know how to use Model-first with DbContext, check out this article . If you want to use Code-first, do not create EDMX and follow this article to match your inheritance.

+2
source

Source: https://habr.com/ru/post/892689/


All Articles