如何在流利的NHibernate中加入表 [英] How to join table in fluent nhibernate
问题描述
我们该怎么做此映射但是流利吗?
how do we do this mapping but fluently?
<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
可以作为我的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
对其进行排序,甚至可以在FormStructure
对象中将DisplayOrder
作为属性使用.
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屋!