6

I see there is a similar question asked here but I don't think it was very clear so I'm creating another : Entity Framework: Inheritance, change object type

I have an Entity Student that inherits from an Entity Person.

At some point a Person could become a Student.

Is there any way in Entity Framework 4 to handle this without a stored procedure or creating a new entity.

Community
  • 1
  • 1
  • 1
    This is a broken object model. In OOP, instances cannot change type. The EF does not add this feature to C#/.NET. You should use composition, not inheritance, for this. – Craig Stuntz Sep 29 '10 at 18:38
  • I was under the impression that it was down to whether a Student IS a Person or whether a Student HAS a Person. Perhaps you could point me to a link where this is defined? –  Sep 30 '10 at 14:23
  • An object can only have one type. So you can't make a type `RedDog` and also have a type `BigDog`, because a `Dog` can be both big *and* red (Clifford). I wrote about this issue here: http://blogs.teamb.com/craigstuntz/2008/07/17/37825/ – Craig Stuntz Sep 30 '10 at 19:39
  • I see this is an old post, based on EF4. Does this limitation yet exists in the last version of Entity (EF6) ? – Corabox Jun 15 '20 at 15:43

1 Answers1

8

No. The EF inheritance does not support this scenario. The best way to create a Student for an existing Person is to use a stored procedure.
Please note that this is not a stored procedure that is wired up to the Student entity through mappings, but a separate one that can be called explicitly from code. Ideally it would be a SP that takes a PersonID as a parameter, inserts a new row into the Student table using that PersonID, and then returns a complete Student so that it can be used immidiately.

Morteza Manavi
  • 33,026
  • 6
  • 100
  • 83