I have the following models defined:
public class Account {
public int Id {get; set;}
... // other account properties
public ICollection<AccountAddress> Addresses {get; set;}
}
public class Address {
public int Id {get; set;}
public string Street1 {get; set;}
public string Street2 {get; set;}
... // other standard address properties
}
public class AccountAddress : Address {
public int AccountId {get; set;}
public DateTime? BeginDate {get; set;}
public string Notes {get; set;}
... // other account address specific properties
}
public class Site {
public int Id {get; set;}
public Address SiteAddress {get; set;}
}
The idea is that any Address can be tied to any Account as well as to any Site. However, any time an Address is associated with an Account, there may be additional information that needs to be stored about that address.
Since AccountAddress inherits from Address, I wanted to use TPT (Table Per Type) inheritance to map Address properties to the Address table and the extended properties to an table of extended address properties.
I tried this:
public class AccountConfiguration : EntityTypeConfiguration<Account>
{
public AccountConfiguration()
{
ToTable("Accounts");
HasKey(a => a.Id);
HasMany(a => a.Addresses).WithMany().Map(x =>
{
x.MapLeftKey("accountId");
x.MapRightKey("addressId");
x.ToTable("accountaddresses");
}
}
}
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
ToTable("addresses");
HasKey(a => a.Id);
... // other property mappings
}
}
public class AccountAddressConfiguration : EntityTypeConfiguration<AccountAddress>
{
public AccountAddressConfiguration()
{
ToTable("addressextended");
... //other property mappings
}
}
The problem here is that there is nothing defined to map accountId into the addressextended table. So if an Address with an Id of 40 is tied to two different accounts, doing the following query:
var _account = _context.Accounts
.Include(a => a.Addresses)
.SingleOrDefault(a => a.Id = 1234);
sometimes gives the extended properties for the wrong account.
What do I need to do in order to get this working?
EDIT:
While the accepted answer did not specifically answer my question (i.e. how can I define a many to many relationship between two entities where one entity is defined with TPT inheritance mapping), it did provide some help in coming up with an acceptable work around.
I did drop the entity inheritance and simply added address as a property of my AccountAddress entity. I then gave AccountAddress (addressextended table) its own primary id. I changed the accountaddresses table to a join table that contains the account id and the accountaddress id (the new id of the addressextended table).
I also had to add a navigation property to AccountAddress for Address and a mapping to go along with that.
I believe you may be able to work around this problem by changing the way you view the “AccountAddress” and “SiteAddress” tables. Instead of them being subclasses of Address, why not make them “join tables”. The AccountAddress entity joins an address and an account, so technically it has to own an account as well as an address. You can also specify other information that are relevant when linking an address with an account (e.g. address type, purge dates, etc).
From what I understand, the goal you are trying to achieve is to link many addresses to many accounts (or sites), and keep some metadata regarding each link.
This model would now denote the following relationship:
1 Account <-> * AccountAddress * <-> 1 Address
This simulates your Many to Many relationship between addresses and accounts by going through the AccountAddress entity.
You should then configure both the Account and Address entities to demonstrate their One-to-Many relationship with something like:
Your can modify your query to look something like: