I know this should not be trivial, but so far couldn't find the resolution...
Working with an EF4 DB-First model, using LINQ-to-Entities with POCOs which will be consumed by an MVC3 app.
I have three entities Customer
, CustomerAdress
and a lookup CustomerAddressType
.
Customer CustomerAddress CustomerAddressType
---------- ---------------- -------------------
CustomerId (PK) CustomerAddressId (PK) CustomerAddressTypeId (PK)
LastName CustomerId (FK) Description (Values: Mailing, Billing)
FirstName CustomerAddressTypeId (FK)
MiddleInitial Address
.... City
State
Zip
StartDate
EndDate
As you can see CustomerAddress
has a FK CustomerAddressTypeId
, which identifies what type of address this is, i.e. Mailing or Billing.
I would like to:
- Have is ability to do something like this:
Customer.CustomerAddress.OfType<MailingAddress>
to get the collection of mailing addresses for the customer. - Have a
CurrentMailingAddress
andCurrentBillingAddress
properties, that would return the single instanceCustomerAddress.OfType<>
with the highestStartDate
andEndDate
in the future. - Would be also nice to take
Address
thruZip
properties and refactor those propertiess into a Complex TypeAddress
.
I tried creating 2 inherited entities off of CustomerAddress
(assuming it is TPH [table-per-hierarchy] strategy):
MailingAddress
and BillingAddress
, CustomerAddressTypeId
being the discriminator. I did this in the model designer, and as soon as I tried adding a second inherited entity, it told me that the properties with those names already existed, and wouldn't let me rename them to match the properties of the first entity.
Any ideas how to accomplish this? Please dumb it down for me :) Thanks!!!