Ejemplo n.º 1
0
 public virtual void AddRole(RoleWithManyToMany role)
 {
     Roles.Add(role);
     role.Users.Add(this);
 }
Ejemplo n.º 2
0
 public virtual void RemoveRole(RoleWithManyToMany role)
 {
     Roles.Remove(role);
     role.Users.Remove(this);
 }
Ejemplo n.º 3
0
        public void Can_User_Add_And_Delete_Roles_In_A_Many_To_Many_bidirectional_Association()
        {
            UserWithManyToMany detachedUser = null;

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                detachedUser = new UserWithManyToMany { Name = "User" };
                unitOfWork.Session.Save(detachedUser);

                for (int i = 0; i < 5; i++)
                {
                    var role = new RoleWithManyToMany { Name = string.Format("Order: {0}", i) };
                    detachedUser.AddRole(role);
                }

                unitOfWork.Commit();
            }

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                var user = unitOfWork.Session.Query<UserWithManyToMany>().Fetch(x => x.Roles).Single(x => x.Id == detachedUser.Id);
                var role = user.Roles.ElementAt(0);
                user.RemoveRole(role);

                unitOfWork.Commit();
            }

            Assert.Equal(6, _sessionFactory.Statistics.EntityInsertCount);
        }