gpt4 book ai didi

nhibernate - NH 3.3 Composite-ID 通过代码映射

转载 作者:行者123 更新时间:2023-12-03 08:29:55 25 4
gpt4 key购买 nike

Diagram

我在这里拉我的头发试图弄清楚如何映射下面列出的 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");
}));
}
}

请参阅 ComponentAsId 映射中的注释

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

最佳答案

您正在寻找的方法称为 ComposedId :

public class UsersRoleMap : ClassMapping<UsersRole>
{
public UsersRoleMap()
{
ComposedId(map =>
{
map.Property(x => x.UserId);
map.Property(x => x.RoleId);
});
}
}

回答你的问题 ComponentAsId作品。你应该有以下类来使用 ComponentAsId方法
public class UsersRoleId
{
public System.Guid UserId { get; set; }
public System.Guid RoleId { get; set; }
}

public class UsersRole
{
public virtual User User { get; set; }
public virtual Role Role { get; set; }
public virtual UsersRoleId Id { get; set; }
}

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

PS:为什么需要映射 UsersRoles table ?我建议您将用户映射到多对多关系的角色。
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());
}
}

关于nhibernate - NH 3.3 Composite-ID 通过代码映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com