Many to Many Insert with POCO Classes

Dec 28, 2010 at 1:40 AM

I have table structure like this:

User 1-------* UserRole *-------1 Role

I have the entities and data projects as in the Expense sample. POCO Classes code as follows:

 public class User
 {
        public virtual Int64 Id { get; set; }
        public string FullName { get; set; }
        public virtual List<Role> Roles { get; set; }
 }

public class Role
{
       public virtual Int64 Id { get; set; }
       public string Name { get; set; }
       public virtual List<User> Users { get; set; }
}

Roles assigning code is as follows:
public void AssignRoles(User user, string[] roleIds)
        {
            user.Roles.Clear();
            foreach (string roleId in roleIds)
            {
                Role role = new RolesDataAccess().SelectRoleById(int.Parse(roleId));
                if (role != null)
                {
                    user.Roles.Add(role);
                }
            }
            this.SaveUser(user);
        }
Data access code is as follows:

public User Update(User user)
        {
            EntityKey key = null;
            object original = null;

            using (DocsFlowContext ctx = new DocsFlowContext())
            {
                key = ctx.CreateEntityKey(User.EntitySetName, user);
                if (ctx.TryGetObjectByKey(key, out original))
                {
                    ctx.ApplyCurrentValues(key.EntitySetName, user);
                }
                ctx.SaveChanges();
            }

            return user;
        }
This "ApplyCurrentValues" doesn't apply the changes to the "Roles" collection.

Does anyone have idea why this thing doesn't update the "Roles" collection?