问题
I'm playing around with entity framework and code first approach. The scenario is this:
- An user can have multiple companies (each company has an address)
- An user can have multiple houses per company (each house has an address)
I'm thinking of two ways I can manage addresses:
- Have an
Address
table with a column forCompanyId
andHouseId
(for companies addresses onlyCompanyId
will be inserted and for houses both Ids will be inserted. - Have a
CompanyAddress
andHouseAddress
tables with the only difference between them being the FK forCompanyId
versusHouseId
.
How would you do it? Is there any other, better options?
回答1:
In EF Core you should use Owned Entity Types for this. In EF 6, use a Complex Type. Both allow you to have an Address type in .NET without having an Address table in your database.
来源:https://stackoverflow.com/questions/60017309/addresses-database-schema-for-multiple-entities