如何在流畅的 nhibernate 中加入表格 [英] How to join table in fluent nhibernate
问题描述
我们如何做这个映射但流利?
<class name="Person" table="People">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Name" />
<join table="Addresses">
<key column="PersonId"/>
<property name="Line1"/>
<property name="Line2"/>
<property name="City"/>
<property name="Country"/>
<property name="ZipCode"/>
</join>
</class>
我知道我可以使用参考",但我不需要相关表中的所有列.我只需要一个属性.
I know i can use 'References' but i don't need all the columns from the related table. i just need one property.
推荐答案
Paco 说的不对.这可以在 Fluent NHibernate 中完成.我自己在网上搜索了很长时间,找不到任何人谈论这个选项,所以我只是玩了一会儿 FNHibernate,终于成功了.
What Paco said is not right. This can be done in Fluent NHibernate. I searched the web myself quite a while, couldn't find anyone talking about this option, so I just played around with FNHibernate a little and finally managed to do it.
这是我的场景:
我有两张桌子 -
"FormFields" => Columns { "FieldId", "FieldName", "FieldType", "DisplayOrder" }
"FormStructure" => Columns { "FormId", "FormType", "FieldId" }
这些是我的实体:
public class FormStructure
{
public virtual Int32 FormId { get; private set; }
public virtual Int32 FormType { get; set; }
public virtual FormField FieldId { get; set; }
}
public class FormField
{
public virtual int FieldId { get; private set; }
public virtual String FieldName { get; set; }
public virtual int? FieldType { get; set; }
public virtual int? DisplayOrder { get; set; }
}
我的查询中有几个方法返回一个 FormStructure
对象列表.我希望这些方法让我按照 FormField
对象中的 DisplayOrder
字段对它们进行排序,并希望 DisplayOrder
在我的 DisplayOrder
中作为一个属性可用code>FormStructure 对象也有其他原因.
I have a couple of methods in my query that return a list of FormStructure
objects. I wanted these methods to give me them ordered by the DisplayOrder
field in the FormField
object, and wanted the DisplayOrder
available as a property in my FormStructure
object for other reasons as well.
这基本上意味着我需要连接表,以便我从 FormStructure 表中检索所有列以及 FormField
表中的 DisplayOrder
列,加入它们在匹配的 FieldId
列上.
This basically means I needed to join the tables so that I would retrieve all the columns from the FormStructure table along with the DisplayOrder
column from the FormField
table, joining them on the matching FieldId
columns.
我做了什么来解决这个问题:
What I did to solve this :
我向我的
FormStructure
对象添加了一个名为 DisplayOrder 的属性.
I added a property called DisplayOrder to my
FormStructure
object.
public virtual int? DisplayOrder { get; set; }
我在我的 FormStructure
映射类中添加了 Join
方法,所以它看起来像这样.
I added the Join
method to my FormStructure
mapping class so it looked like this.
public class FormStructureMap : ClassMap<FormStructure>
{
public FormStructureMap()
{
Table("FormStructure");
Id(x => x.Id);
Map(x => x.FormType);
References(x => x.Schedule).Column("ScheduleId");
References(x => x.Field).Column("FieldId");
Map(x => x.IsMandatory).Nullable();
Join("FormFields", m =>
{
m.Fetch.Join();
m.KeyColumn("FieldId");
m.Map(t => t.DisplayOrder).Nullable();
});
}
}
Join
方法显然会在您在 Join 中的 KeyColumn 方法中定义的列上的两个表之间进行连接.
The Join
method will obviously join between the two tables on the column you defined in the KeyColumn method within the Join.
这也将删除一些具有空值的行.为了避免这种情况(我最近遇到了这个),您可以在 Join
方法中添加 m.Optional();
.
This will also remove some of the rows that have null values. In order to avoid this (I ran into this recently) you can add m.Optional();
inside the Join
method.
我现在可以检索 FormStructure
对象的列表,按 DisplayOrder
对它们进行排序,甚至可以将 DisplayOrder
作为 中的一个属性使用>FormStructure
对象.
I could now retrieve a list of FormStructure
objects, order them by DisplayOrder
and even have DisplayOrder
available as a property in the FormStructure
object.
return session.CreateCriteria<FormStructure>()
.Add(Expression.Eq("FieldName", fieldName))
.AddOrder(Order.Asc("DisplayOrder"))
.List<FormStructure>();
以前不可能这样做,因为它不会识别我在那里的 Order 子句中的 DisplayOrder
列.
This couldn't have been done before, because it wouldn't have recognized the DisplayOrder
column in the Order clause I have there.
这篇关于如何在流畅的 nhibernate 中加入表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!