Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 多对多映射中的Fluent nHibernate问题_Asp.net Mvc_Nhibernate_Fluent Nhibernate_Mapping - Fatal编程技术网

Asp.net mvc 多对多映射中的Fluent nHibernate问题

Asp.net mvc 多对多映射中的Fluent nHibernate问题,asp.net-mvc,nhibernate,fluent-nhibernate,mapping,Asp.net Mvc,Nhibernate,Fluent Nhibernate,Mapping,我有一个有趣的问题发生在我身上,我不知道我做错了什么,我在使用Fluent nHibernate和MVC 3,我得到了多对多关系的用户、角色和UsersinRole表 用户映射: Id(user => user.UserID).GeneratedBy.Guid(); Map(user => user.UserName).Not.Nullable(); Map(user => user.Password).Not.Nullable();

我有一个有趣的问题发生在我身上,我不知道我做错了什么,我在使用Fluent nHibernate和MVC 3,我得到了多对多关系的用户、角色和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(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(x=>x.Roles).Table(“tbluseringroles”)
.ParentKeyColumn(“用户ID”)
.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();
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(x=>x.Users)
.表格(“TBluserin角色”)
.ParentKeyColumn(“RoleID”)
.ChildKeyColumn(“用户ID”)
.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);
    }
公共虚拟Guid用户ID{get;set;}
公共虚拟字符串用户名{get;set;}
公共虚拟字符串密码{get;set;}
公共虚拟字符串全名{get;set;}
公共虚拟字符串电子邮件{get;set;}
公共虚拟时间跨度LastLogin{get;set;}
公共虚拟布尔是活动的{get;set;}
公共虚拟日期时间创建日期{get;set;}
公共虚拟IList角色{get;set;}
公共用户()
{
角色=新列表();
}
公共虚拟void AddRoles(角色)
{
role.Users.Add(此);
角色。添加(角色);
}
角色实体:

 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>();
    }
公共虚拟字符串RoleName{get;set;}
公共虚拟布尔是活动的{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟IList用户{get;set;}
公共虚拟IList角色{get;set;}
公共角色()
{
用户=新列表();
}
现在的问题是,当我删除任何角色时,它会删除UserInRole表中角色与用户的关联,并删除与我删除的角色相关的所有用户。如果我删除用户,同样的事情会反过来发生


有人知道问题出在哪里吗

您指定了从用户到角色的
.Cascade.All()
,因此当您删除用户时,NHibernate当然会删除所有角色。如果不应该使用
.Cascade.All()
,请不要使用


您指定了从角色到用户的
.Cascade.SaveUpdate()
,删除角色时不应删除任何用户。你确定它删除了用户,而不仅仅是用户的关联吗?

我犯了一些错误,现在它工作正常,这对我有帮助。