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");
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(); }
source share