0

我有以下用户实体:

public class User
{
    public virtual int Id { get; set; }

    public virtual string Username { get; set; }

    public virtual string UsernameLowercase { get { return Username.ToLowerInvariant(); } }

    public virtual string Password { get; set; }

    public virtual string Email { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        var otherUser = obj as User;
        return (0 == string.CompareOrdinal(UsernameLowercase, otherUser.UsernameLowercase));
    }

    public override int GetHashCode()
    {
        return UsernameLowercase.GetHashCode();
    }
}

和以下角色实体:

public class Role
{
    public virtual int Id { get; set; }

    public virtual string RoleName { get; set; }

    public virtual string RoleNameLowercase { get { return RoleName.ToLowerInvariant(); } }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        var otherRole = obj as Role;
        return (0 == string.CompareOrdinal(RoleNameLowercase, otherRole.RoleNameLowercase));
    }

    public override int GetHashCode()
    {
        return RoleNameLowercase.GetHashCode();
    } 
}

因为可以为用户分配许多角色,所以我创建了第三个名为 UserInRole 的实体,如下所示:

public class UserInRole
{
    public virtual User User { get; set; }

    public virtual Role Role { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        var other = obj as UserInRole;
        return (Role == other.Role && User == other.User);
    }

    public override int GetHashCode()
    {
        return User.GetHashCode() ^ Role.GetHashCode();
    }
}

我想使用流利的 nhibernate 在 UsersInRoles 表和 User 和 Role 表之间进行映射,以便当我删除用户时,UsersInRoles 表中定义的所有用户角色也会自动删除。这是我当前的 UserInRole 实体映射类:

public class UserInRoleMapping : ClassMap<UserInRole>
{
    public UserInRoleMapping()
    {
        Table("UsersInRoles");

        CompositeId().
            KeyReference(x => x.User,k => k.Not.Lazy(), "UserId").
            KeyReference(x => x.Role, k => k.Not.Lazy(), "RoleId");

    }
}

感谢您的帮助。

4

1 回答 1

1

所以在搜索了一段时间后,我发现了这个讨论。我将我的用户实体更改为以下内容(注意新的受保护角色属性):

public class User
{
    private IList<UserInRole> _roles = new List<UserInRole>(); 

    public virtual int Id { get; set; }

    public virtual string Username { get; set; }

    public virtual string UsernameLowercase { get { return Username.ToLowerInvariant(); } }

    public virtual string Password { get; set; }

    public virtual string Email { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        var otherUser = obj as User;
        return (0 == string.CompareOrdinal(UsernameLowercase, otherUser.UsernameLowercase));
    }

    public override int GetHashCode()
    {
        return UsernameLowercase.GetHashCode();
    }

    protected virtual IList<UserInRole> Roles
    {
        get { return _roles; }
        set
        {
            value.ThrowIfNull("value");
            _roles = value;
        }
    } 
}

用户映射已更改为包含 HasMany 定义,如下所示:

public abstract class UserMapping: ClassMap<User>
{
    protected UserMapping()
    {
        Table("Users");

        Map(x => x.Username).Column("Username").Length(20).Not.Nullable().Unique();
        Map(x => x.Password).Column("Password").Length(20).Not.Nullable();
        Map(x => x.Email).Column("Email").Length(50).Not.Nullable();

        //This line was missing
        HasMany<UserInRole>(Reveal.Member<User>("Roles")).
            Table("UsersInRoles").
            KeyColumn("UserId").
            Inverse().
            Cascade.Delete();
    }
}

现在,当我删除用户时,角色也会被删除而不会出现错误。当我查看 nhibernate 日志时,我看到单个 Delete(user) 调用的 2 个删除语句,如下所示:

13:18:39.950 [7] DEBUG NHibernate.SQL - DELETE FROM UsersInRoles WHERE UserId = :p0 AND RoleId = :p1;:p0 = 7 [Type: Int32 (0)], :p1 = 3 [Type: Int32 (0)]
13:18:39.983 [7] DEBUG NHibernate.SQL - DELETE FROM Users WHERE Id = :p0;:p0 = 7 [Type: Int32 (0)]

希望这可以帮助某人。欢迎您发表意见。

于 2012-10-23T11:49:33.120 回答