映射两个表中的字段以创建单个实体 [英] Map fields from two tables to create a single entity

查看:145
本文介绍了映射两个表中的字段以创建单个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一项功能,以使我们的每个客户都能够为其帐户定义自定义注册表格,在为其中一个对象创建Fluent NHibernate映射时遇到了一些障碍

I'm working on a feature to add the ability for each of our customers to define a customized registration form for their account and I've run into a bit of a roadblock in creating my Fluent NHibernate mapping for one of the objects.

有两个涉及[RegistrationField][AccountRegistrationField]的表. [RegistrationField]包含可供选择的所有字段的静态列表,以及有关如何呈现该字段的一些信息(例如,应为文本框,选择框还是复选框). [AccountRegistrationField]包含已为每个帐户选择的字段以及有关该字段的任何帐户特定信息.该表存储了字段显示的顺序,字段的默认值(如果选择了该字段的默认值)以及应该为该字段显示的标签.

There are two tables involved [RegistrationField] and [AccountRegistrationField]. [RegistrationField] contains a static list of all of the fields that are available to pick from as well as some information on how to render the field (e.g. should it be a textbox, select box or checkbox). [AccountRegistrationField] contains the fields that have been selected for each account as well as any account specific information about the fields. This table stores the order the fields should be displayed in, the default value for the field if one is selected and the label that should be displayed for the field.

两个表的基本结构是:

[RegistrationField]
fieldID (PK, int, not null)
fieldName (varchar(50), not null)
fieldType (varchar(50), not null)
htmlID (varchar(50), not null)

[AccountRegistrationField]
ID (PK, int, not null)
accountID (FK, int, not null)
fieldID (FK, int, not null)
isRequired (bit, not null)
priority (int, not null)
label (varchar(50), not null)
defaultValue (varchar(50), not null)

我想做的是创建一个对象来表示[AccountRegistrationField]中的数据,但是它也包含来自[RegistrationField]的几个字段.该对象将如下所示:

What I would like to do is create an object to represent the data in [AccountRegistrationField] but that also contains a couple of fields from [RegistrationField]. The object would look something like this:

public class UserRegistrationField
{
    public virtual int ID { get; set; } //from AccountRegistrationField
    public virtual int AccountID { get; set; } //from AccountRegistrationField
    public virtual string DefaultValue { get; set; } //from AccountRegistrationField
    public virtual int FieldID { get; set; } //from AccountRegistrationField
    public virtual string HtmlID { get; set; } //from RegistrationField
    public virtual bool IsRequired { get; set; } //from AccountRegistrationField
    public virtual string Label { get; set; } //from AccountRegistrationField
    public virtual int Priority{ get; set; } //from AccountRegistrationField
    public virtual string FieldType { get; set; } //from RegistrationField
}

提供我想要的内容的SQL查询是:

The SQL query to give me what I want is:

SELECT ar.id
    ,ar.accountID
    ,ar.defaultValue
    ,ar.fieldID
    ,rf.htmlID
    ,ar.isRequired
    ,ar.label
    ,rf.fieldType
FROM AccountRegistrationFields ar
INNER JOIN RegistrationFields rf ON rf.fieldID = ar.fieldID
WHERE ar.accountID = @AccountID
ORDER BY ar.priority

但是我不太确定如何使用Fluent NHibernate映射它,以得到相同的结果.

but I'm not really sure how to map this using Fluent NHibernate to give me the same result.

有什么想法可以实现此目的而不必将我需要的[RegistrationField]值存储在[AccountRegistrationField]中吗?

Any ideas how I can make this happen without having to store the values I need from [RegistrationField] in [AccountRegistrationField]?

推荐答案

您可以使用加入以实现这一目标.

You can use join to accomplish that.

我使用XML,但是等效的Fluent方法可能称为Join.

I use XML, but the equivalent Fluent method is probably called Join.

这篇关于映射两个表中的字段以创建单个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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