开发者

Delete on Many to Many Relationship using Fluent Nhibernate

User Class

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);
        }
    }

Role Class

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 Cla开发者_如何学Goss

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 Class

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();
        }
    }

My questions are:

  • I want to delete all roles of particular user? [Answered by danyolgiax]
  • Get roles of user

Can Any one guide me..


try .Inverse() on User (not on Role) and then:

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

update

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

IList<Role> role= user.Roles;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜