Entity Framework: Inheritance, change object type
Asked Answered
A

5

14

Let's say, I have 2 classes in the model: User (mapped to USERS table) and PrivilegedUser (inherits User, additional info is stored in PRIVILEGEDUSERS table).

Now, I have a row in USERS (and instance of User) and need to convert that user to PrivilegedUser (i.e. to create a record in PRIVILEGEDUSERS with the same Id). Is there a way to do this without Delete/Insert?

The problem is you don't have PRIVILEGEDUSERS representation in the model, so you cannot create only that part of PrivilegedUser.


It was just an example. PrivilegedUser may have some discount or personal manager or whatever in addition to ordinary User properties. In the same time, there are other tables which need to reference users regardless of concrete User type. I've implemented it using Table-per-Type inheritance mode. In the database level it's very simple to convert users from one type to another (you just need to insert or delete record from extension table). But in EF you have only UserSet which stores both User and PrivilegedUser objects. That's why I ask is it possible to replace existing User object with PrivilegedUser keeping existing Id and without deleting record from USERS table.

Annora answered 6/8, 2009 at 15:4 Comment(0)
W
3

No you cannot.

As explained by this article, EF (3.5) does not support this feature. You must use stored procedure to accomplish this.

Wheeled answered 28/2, 2010 at 10:38 Comment(0)
M
1

You need to change your world view. Your view is that you have standard users with standard privileges and super users with additional privileges. The privileges aren't enumerated, they are implicit.

The new world view is that you maintain a list of all privileges, both standard and super and then in a mapping table you create a many to many map of which users have which privileges. When a user is granted super privileges, you just add mappings for the appropriate privileges to the mapping table. You don't need a PrivilegedUser class, just a list of privileges in the User class. The privileges may be either standard or super.

Malaise answered 2/10, 2010 at 20:2 Comment(0)
M
0

It seems wrong that you have two tables representing users.

Would it not be better to have a Users table (for all users) and then a UserPrivileges table, representing what they are allowed to do? This way, no deletes/Inserts are needed, and you can reference just one table for users.

A third table can be used to represent the actual privileges.

Users
Id Username ...

UserPrivileges
UserId PrivilegeId

Privileges
Id Description

Massorete answered 6/8, 2009 at 15:10 Comment(6)
This is correct. An instance should/can never change type. So what happens when a user becomes privileged or non-privileged?Lilia
@Craig, any news? I ran into the same question.Suffruticose
@Shimmy, "news?" What news? If you think your object needs to change type, then you are probably misusing inheritance. @DanDan's answer is still a better idea.Lilia
@Craig, I have a Person table and an Employee table (inherits Person), I created a Person, 6 years later this person became an employee, is there a way I can attach an Employee record to this existing Person so that he gets the additional features of Employee?Suffruticose
@Shimmy, not with the EF. You could do it with raw SQL, because SQL doesn't "know" about class inheritance. The best option, in my opinion, is to charge to a model where the Person to Employee relationship is one of aggregation rather than inheritance, for exactly the reason you describe.Lilia
@Craig, I guess what I will do is indeed using a sproc for this 'once per six years' situation, and reload (as Employee) it from the server.Suffruticose
F
0

Regarding inheritance in EF take a look at this site, which explains the three different ways to use inheritance in EF.

http://blogs.microsoft.co.il/blogs/gilf/archive/2010/01/20/entity-framework-inheritance-types.aspx

Flavia answered 22/3, 2010 at 8:40 Comment(0)
D
0

As said, you cannot. Either by stored procedures or by custom insert/update query. I had similar problem, now I'm using solution that i described in this answer: https://mcmap.net/q/902346/-entity-framework-4-tph-inheritance-how-to-change-one-type-into-another

Diet answered 7/2, 2015 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.