Dealing with "immutable" entities

Posts   
 
    
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 30-Mar-2009 01:43:24   

Hi,

ok theres not really such a thing as an "immutable" entity but there are entities which I prefer to treat as immutable. Mainly these are M2M entities (like a UserRole - User 2 Role).

I'm writing a user editing page at the moment and I thought to ask this. When editing an existing user I am fetching the user:


var users = from u in meta.User
                            where u.UserId == userid
                            select u;
                
                users = users.WithPath(opath=>opath.Prefetch(p=>p.RoleCollectionViaUserRole));

                var user = users.First();

In this scenario I am only allowing one role to be associated with the user but I always use the same design:

User.UserId - UserRole.UserId UserRole.RoleId - Role.RoleId

So I can delete the existing UserRole entity like this (in a transaction):


adapter.DeleteEntity(user.UserRoleUsingUserId[0]);
user.UserRoleUsingUserId.Clear();

or


adapter.DeleteEntitiesDirectly(typeof(UserRoleEntity), new RelationPredicateBucket(UserRoleFields.UserId == user.UserId));
user.UserRoleUsingUserId.Clear();

and then I add a new one:


user.UserRoleUsingUserId.Add(new UserRoleEntity() { RoleId = Convert.ToInt32(RoleId.SelectedValue) });

then I save:


adapter.SaveEntity(user, true, true);

Now to my question, is there someway I can just mark the original UserRoleEntity for deletion without removing it from the collection and manually deleting it. As I am using the recurse feature of adapter.SaveEntity it'd be nice if in addition to recursing Inserts and Updates it could also do deletes.

(.Net 3.5, LLBL 2.6 Feb 6th Build, SQL 2005)

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Mar-2009 11:00:14   

The entityCollection doesn't have this functionality, but you can use a UnitOfWork to set entities for deleteion, and some to be saved. That's mainly why the UnitOfWork was invented, to act as a container or a big Collection which it can hold entities marked for deletion and for other actions too.

Also please read the following: Tracking entity remove actions

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 30-Mar-2009 14:31:44   

Thanks Walaa, this is awesome. Maybe I should RTFM sometime smile

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Mar-2009 15:32:45   

We know the manual is so huge to be read up front. That's our job here to point you to the right direction. simple_smile