使用Fluent Nhibernate删除多对多关系

时间:2011-07-12 07:46:00

标签: nhibernate asp.net-mvc-3 fluent-nhibernate

用户类

public class User
    {
        public virtual Guid UserID { get; set; }

        public virtual string UserName { get; set; }

        public virtual string Password { get; set; }

        public virtual string FullName { get; set; }

        public virtual string Email { get; set; }

        public virtual TimeSpan LastLogin { get; set; }

        public virtual bool IsActive { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual IList<Role> Roles { get; set; }

        public User()
        {
            Roles = new List<Role>();

        }
        public virtual void AddRoles(Role role)
        {
            role.Users.Add(this);
            Roles.Add(role);
        }
    }

角色等级

public class Role
    {

        public virtual int? RoleID { get; set; }

        public virtual string RoleName { get; set; }
        public virtual bool IsActive { get; set; }

        public virtual string Description { get; set; }
        public virtual IList<User> Users { get; set; }
        public virtual IList<Role> Roles { get; set; }

        public Role()
        {
            Users = new List<User>();
        }
    }

UserMap类

public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("tblUsers");

            Id(user => user.UserID).GeneratedBy.Guid();
            Map(user => user.UserName).Not.Nullable();
            Map(user => user.Password).Not.Nullable();
            Map(user => user.FullName).Not.Nullable();
            Map(user => user.Email).Not.Nullable();
            //Map(user => user.LastLogin).Nullable();
            Map(user => user.IsActive).Not.Nullable();
            Map(user => user.CreationDate).Not.Nullable();


            HasManyToMany<Role>(x => x.Roles).Table("tblUserInRoles")
                                                .ParentKeyColumn("UserID")
                                                .ChildKeyColumn("RoleID")
                                                .Cascade.All()
                                                //.AsSet()
                                                //.Inverse()
                                                .Not.LazyLoad();


        }
    }

RoleMap类

public class RoleMap : ClassMap<Role>
    {
        public RoleMap()
        {
            Table("tblRoles");
            Id(role => role.RoleID).GeneratedBy.Identity();
            Map(role => role.RoleName).Not.Nullable();
            Map(role => role.IsActive).Not.Nullable();
            Map(role => role.Description).Not.Nullable();

            HasManyToMany<User>(x => x.Users)
                .Table("tblUserInRoles")
                .ParentKeyColumn("RoleID")
                .ChildKeyColumn("UserID")
                .Cascade.All()
                .Inverse()
                .Not.LazyLoad();
        }
    }

我的问题是:

  • 我想删除特定用户的所有角色? [danyolgiax回答]
  • 获取用户角色

任何人都可以指导我......

1 个答案:

答案 0 :(得分:3)

.Inverse()上尝试User(不在Role上)然后:

user.Roles.Clear();
yourNhSession.SaveOrUpdate(user);

<强>更新

User user= yourNhSession.Get<User>(userId);

IList<Role> role= user.Roles;