locked
Casting Entity Framework Entities in the “Wrong” Direction RRS feed

  • Question

  • I am using the Entity Framework and have an inheritance structure with a base Entity (let's call it Customer) and a derived Entity, let's call it AccountCustomer. The difference is that an AccountCustomer has extra details (such as payment terms etc.) stored in a separate table in the database and therefore extra properties in the Entity.

    I want to allow users to 'promote' a specific Customer to be an AccountCustomer. I need to keep the same primary key (a composite key visible to users and used as the customer reference).

    At the moment my feeling is that calling a stored procedure to create the additional record in the Accounts table is the only way to go, but up to now we have not bypassed the Entity Framework so would prefer to avoid this technique if possible.

    Has anybody any Entity Framework focussed solutions?

    Thursday, March 26, 2009 11:46 AM

Answers

  • Unfortunately, that's how I'm doing it also, with a stored procedure. I'd be curious to see another way as well, but the sproc is the best solution I could come up with and that's even what I used in my book on EF. Even then, I have to remove the Customer from the cache and requery the new CustomerAccount since you can't cast one entity type to another.

    julie
    • Marked as answer by dwalker3 Thursday, March 26, 2009 2:19 PM
    Thursday, March 26, 2009 1:38 PM