2

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 and CurrentBillingAddress properties, that would return the single instance CustomerAddress.OfType<> with the highest StartDate and EndDate in the future.
  • Would be also nice to take Address thru Zip properties and refactor those propertiess into a Complex Type Address.

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!!!

Romias
  • 13,783
  • 7
  • 56
  • 85
Eric
  • 139
  • 2
  • 11

1 Answers1

1

It is not such trivial. TPH will be possible but you must place all properties to the base CustomerAddress and derive two sub entities which will not hold any property because all properties are shared (= must be in the parent). You will use CustomerAddressTypeId as discriminator and because of that you will not be able to map this field as property in the entity. I'm also not sure if you can have the field both in discriminator and association mapping (that is actually nice homework for me). If not you will not be able to map association between CustomerAddress and CustomerAddressType.

Both CurrentMailingAddress and CurrentBillingAddress are computed properties and they are not part of mapping. It is up to you to implement their logic in your partial part of Customer entity.

I don't understand the last point with Zip and complex type.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thank you Ladislav. I guess I will start backwards in replying to your post. :) 1) `Address`, `City`,`State`,`Zip` - I would like to refactor into a complex type, but wasn't sure if it will cause me grief down the road (I think it should be ok). 2) The reason why I mentioned Current*Address, is that's where I would like to leverage the CustomerAddress.OfType<>. 3) I will give it a shot and let you know how I make out. – Eric Apr 22 '11 at 13:18
  • Ok, I created 2 entities `CustomerBillingAddress` and `CustomerMailingAddress` with base type `CustomerAddress`. Set the table mapping to `CustomerAddress` and discriminator column for each entity `CustomerAddressTypeId` with appropriate value. Then, when I go to validate the model, I get the following error: – Eric Apr 22 '11 at 14:04
  • `Error 1 Error 3023: Problem in mapping fragments starting at lines 1146, 1169, 1175:Column CustomerAddresses.AddressTypeId has no default value and is not nullable. A column value is required to store entity data. An Entity with Key (PK) will not round-trip when: (PK is in 'CustomerAddresses' EntitySet AND Entity is type [Entities.CustomerAddress])`. I tried removing the discriminator column 'CustomerAddressTypeId` from the base entity `CustomerAddress`, thus breaking the relationship with `CustomerAddressType`, still the same message shows up. – Eric Apr 22 '11 at 14:04
  • Ok, I think I resolved the model validation error... Had to: 1) on entity `CustomerAddress` change the property to Abstract: True (to create abstract class); 2) Delete the entity `CustomerAddressType`; 3) Remove discriminator column `CustomerAddressTypeId` from the base entity `CustomerAddress`. Didn't get a chance yet to verify in the code that everything is working as expected, but will post soon. – Eric Apr 22 '11 at 14:39
  • Ok, I finally was able to verify, and it works, but Ladislav, as you mentioned, I can't have it both ways for `CustomerAddressTypeId` to be both association and discriminator. So I had to lose association, which is not a big deal. – Eric Apr 29 '11 at 17:21
  • Ladislav, as for the implementation of `CurrentMailingAddress` and `CustomerBillingAddress` properties in the partial class of `Customer`. That would require for me to implement via Linq to Entities actual implementation. Wouldn't that violate the separation of concerns principle (the reason why I went with naked POCOs)? – Eric Apr 29 '11 at 17:25