Map fields from two tables to create a single entity

江枫思渺然 提交于 2019-12-24 19:07:16

问题


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.

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.

The basic structure of the two tables is:

[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)

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
}

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

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

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


回答1:


You can use join to accomplish that.

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



来源:https://stackoverflow.com/questions/4305934/map-fields-from-two-tables-to-create-a-single-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!