流畅的NHibernate问题 [英] Fluent NHibernate question

查看:18
本文介绍了流畅的NHibernate问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有两个表,Users"和UserRoles".这两个表的结构如下(表 - 列):

Let's say you have two tables, "Users" and "UserRoles". Here's how the two tables are structured (table - columns):

用户 - 用户 ID(整数)

Users - UserID (int)

UserRoles - 用户 ID(整数)、角色(字符串)

UserRoles - UserID (int), Role (string)

我想要的是我的域中的用户"类具有角色 IList.我如何构建我的 Fluent NHibernate 映射来实现这一点?

What I want is for my "User" class in my domain to have an IList of roles. How do I construct my Fluent NHibernate mapping to achieve this?

推荐答案

您要找的是一组元素,在标准 hbm 映射中是:

What you're looking for is a of a set of elements, which in standard hbm mapping is:

<set name="Roles" table="UserRoles">
  <key column="UserID" />
  <element column="Role" />
</set>

对于 Fluent NHibernate,您可以这样映射:

For Fluent NHibernate you can map this like so:

HasMany<string>(x => x.Roles)
  .AsElement("Role");

您可能还需要使用 WithKeyColumn(string) 指定键名.

You may need to also specify the key name using WithKeyColumn(string).

这篇关于流畅的NHibernate问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆