views:

425

answers:

5

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.

A: 

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

DanDan
This is correct. An instance should/can never change type. So what happens when a user becomes privileged or non-privileged?
Craig Stuntz
A: 

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.

Rouslan Minasian
Please delete this post and paste its contents as a comment to the relevant answer (or your question). This is not an answer, it's a comment.
+2  A: 

No you cannot.

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

Tri Q
A: 

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

Michael
A: 

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.

J Edward Ellis