views:

65

answers:

2

Hello:

I can't figure out why calling SaveChanges() on the following code results in no changes to the objects I attached:

public void Update()
{
AccountUser accountUser = new AccountUser();

// update
using (var db = new MedicalSystemDBEntity())
{
    var query = from user in db.AccountUsers   
                where user.id == this.UserID
                select user;

    if (query.Count() > 0)
    {
        accountUser = query.First();

        accountUser.AccountRoles.Load();
        accountUser.SecurityNavigationNodes.Load();

        // delete existing user roles before re-attaching
        if (accountUser.AccountRoles.Count > 0)
        {
            foreach (AccountRole role in accountUser.AccountRoles.ToList())
            {
                accountUser.AccountRoles.Remove(role);
            }
        }

        db.SaveChanges();

        // get roles to add
        List<int> roleIDs = new List<int>();

        foreach (UserRole r in this.AccountRoles)
        {
            roleIDs.Add(r.RoleID);
        }

        var roleEntities = from roles in db.AccountRoles
                           where roleIDs.Contains(roles.id)
                           select roles;

        accountUser.AccountRoles.Attach(roleEntities);

        accountUser.username = this.Username;
        accountUser.firstname = this.FirstName;
        accountUser.middlename = this.MiddleName;
        accountUser.lastname = this.LastName;
        accountUser.enabled = this.Enabled;

        if (this.LastActivityDate != null || this.LastActivityDate != DateTime.MinValue)
            accountUser.lastactivitydate = this.LastActivityDate;

        db.SaveChanges();
    }
}

}

In the debugger, I see that the correct roleEntities are being loaded, and that they are valid objects. However, if I use SQL Profiler I see no UPDATE or INSERT queries coming in, and as a result none of my attached objects are being saved.

A: 

Off the top of my head, shouldn't you do SaveChanges() after you've removed the roles from the account? Aren't you just removing the roles attached to the user, then reattaching the same ones? Since its saving the changes, nothing would have changed would it?

Paul
I tried calling SaveChanges() after removing, then again after re-attaching. The thing is, I'm removing the old roles and adding new ones. There may be more, less or different roles that are being reattached than those that were there before.For example, the user may have no roles but then when reattaching might add a new role to the user.
Well if you're deleting shouldn't you be doing db.DeleteObject(role) and not just removing them from the context?
Paul
I tried doing a DeleteObject, but that removes the role... I want the role to remain, because it may be associated with other users... I just want the association of AccountUser <-> AccountRole to be removed. In normal SQL, I would just remove the entry in the table that joins AccountUser and AccountRole. I'm not trying to remove the actual role itself
+1  A: 

They're not saving because you change the entities before attaching them. Changes are tracked by the context (usually), so changes to detached entities aren't tracked. Hence, nothing to save.

Craig Stuntz
Well, I'm not trying to change the role itself... I'm trying to change the role associated with the user. Maybe I'm misunderstanding something?The user and account roles have a many-to-many relationship using a join table. The join table isn't accessible directly as it forms the navigation property (right?). So, adding a new role to a user should add an entry in this table.