多对多映射中的 Fluent nHibernate 问题
我遇到了一个有趣的问题,我不知道我做错了什么,我使用 Fluent nHibernate 和 MVC 3,我得到了 User 、 Roles 和一个用于多对多关系的 UsersinRole 表。
用户映射:
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")
.ParentKeyColumn("UserID")
.ChildKeyColumn("RoleID")
.Cascade.All()
.Not.LazyLoad();
角色映射:
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();
用户实体:
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 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>();
}
现在的问题是,当我删除任何角色时,它会删除 UserInRole 表中角色与用户的关联,并删除与我删除的角色相关的所有用户。如果我删除用户,同样的事情也会发生相反的情况。
有谁知道是什么问题吗?
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")
.ParentKeyColumn("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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
.Cascade.All()
从 User 指定为 Role,因此当您删除 User 时,NHibernate 当然会删除所有 Role。如果不应该发生这种情况,请不要使用.Cascade.All()
。您将角色指定为用户的
.Cascade.SaveUpdate()
,删除角色时不应删除任何用户。您确定它删除用户而不仅仅是与用户的关联吗?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?