Nhibernate NH 3.3按代码的复合ID映射

Nhibernate NH 3.3按代码的复合ID映射,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,我在这里发毛,试图找出如何映射下面列出的UsersRoles表。我的秃头看起来不太好看,所以请帮帮我:) //这是实体 public class UsersRole { public UsersRole() { } public virtual User User { get; set; } public virtual Role Role { get; set; } public virtual System.Guid UserId { get; set; }

我在这里发毛,试图找出如何映射下面列出的UsersRoles表。我的秃头看起来不太好看,所以请帮帮我:)

//这是实体

public class UsersRole
{
    public UsersRole() { }
    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
    public virtual System.Guid UserId { get; set; }
    public virtual System.Guid RoleId { get; set; }


}
//这是到目前为止的地图

public class UsersRoleMap : ClassMapping<UsersRole>
{
    public UsersRoleMap()
    {
        Table("UsersRoles");
        Lazy(true);

       // ComponentAsId(); How does this work??


        Property(x => x.UserId, map => map.Column(c =>
            {
                c.Name("UserId");
                c.NotNullable(true);
                c.Length(30);
                c.SqlType("uniqueidentifier");
            }));
        Property(x => x.RoleId, map => map.Column(c =>
            {
                c.Name("RoleId");
                c.NotNullable(true);
                c.Length(30);
                c.SqlType("uniqueidentifier");
            }));
    }
}
public类UsersRoleMap:ClassMapping
{
public UsersRoleMap()
{
表(“用户表”);
懒惰(真);
//ComponentAsId();这是如何工作的??
属性(x=>x.UserId,map=>map.Column(c=>
{
c、 名称(“用户ID”);
c、 不可为空(true);
c、 长度(30);
c、 SqlType(“唯一标识符”);
}));
属性(x=>x.RoleId,map=>map.Column(c=>
{
c、 名称(“RoleId”);
c、 不可为空(true);
c、 长度(30);
c、 SqlType(“唯一标识符”);
}));
}
}
请参见ComponentAsId映射中的注释


如果有人能让我走上正轨,请提前感谢

您真的需要将
UsersRole
作为一个实体吗?通常,只有在数据库表中除了两个ID之外还有一些额外的列时,才能创建多对多实体

此外,即使需要创建单独的实体,也不需要具有
UserId
RoleId
属性。拥有
用户
角色
属性就足以映射到NHibernate

请查看多对多映射,并按如下方式定义用户实体:

public class User
{
    // other properties
    public virtual IList<Role> Roles { get; set; }
}

这里还有一个。

您正在寻找的方法称为
ComposedId

public class UsersRoleMap : ClassMapping<UsersRole>
{
    public UsersRoleMap()
    {
        ComposedId(map => 
        {
            map.Property(x => x.UserId);
            map.Property(x => x.RoleId);
        });
    }
}
现在您可以将UsersRole.Id映射为
ComponentAsId

public class UsersRoleMap : ClassMapping<UsersRole>
{
    public UsersRoleMap()
    {
        ComponentAsId(x => x.Id);
    }
}

以下是通过代码实现NHibernate映射的复合键映射的两种方法

ComponentAsId(x => x.Key, m =>
{
    m.Property(x => x.KeyPart);
    // etc.
});

ComposedId(m =>
{
    m.Property(x => x.KeyPart);
    // other key parts
});

好极了,先生。谢谢你的信息。我将映射为多对多。是的,您的复合id是正确的,我真的不需要将usersroles表映射为一个实体。谢谢你在ComponentAsId上的例子。这对我来说仍然不起作用。我获取外键(FK5F7A4833608B61EC:Roles[elt])必须与引用的主键(Roles[user_key,elt])具有相同的列数。我非常确定它是ComposedId而不是CompositeId。
public class UsersRoleMap : ClassMapping<UsersRole>
{
    public UsersRoleMap()
    {
        ComponentAsId(x => x.Id);
    }
}
public class UsersMap : ClassMapping<User>
{
    public UsersMap()
    {
        Set(x => x.Roles, x => { }, x => x.ManyToMany());
    }
}

public class RolesMap : ClassMapping<Role>
{
    public RolesMap()
    {
        Set(x => x.Users, x => { }, x => x.ManyToMany());
    }
}
ComponentAsId(x => x.Key, m =>
{
    m.Property(x => x.KeyPart);
    // etc.
});

ComposedId(m =>
{
    m.Property(x => x.KeyPart);
    // other key parts
});