开发者

Fluent nHibernate Issue in Many to Many Mappings

I have an interesting issue happening with me , i do not know what i m doing wrong, i m using Fluent nHibernate with MVC 3, i got User , Roles , and a UsersinRole table for many to many relationship.

User Mapping:

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.IsActive).Not.Nullable();
        Map(user => user.CreationDate).Not.Nullable();

        HasManyToMany<Role>(x => x.Roles).Table("tblUserInRoles")
                                            .ParentKeyColum开发者_Python百科n("UserID")
                                            .ChildKeyColumn("RoleID")
                                            .Cascade.All()
                                            .Not.LazyLoad();

Roles Mapping:

 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.SaveUpdate()
            .Inverse()
            .Not.LazyLoad();

User Entity:

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 Entity:

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

Now the problem is that when i m deleting any role , it deletes the association of roles with user in UserInRole table and as well as delete all user related to the role i m deleting. same thing happens in inverse if i delete the user.

Does any one know what is the issue ?


You specified .Cascade.All() from User to Role, so of course NHibernate is deleting all Roles when you delete a User. Don't use .Cascade.All() if that shouldn't happen.

You specified .Cascade.SaveUpdate() from Role to User, no User should be deleted when you delete a Role. Are you sure that it deletes the Users and not only the associations to Users?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜