Cur*_*Fro 21 nhibernate fluent
我们怎么做这个映射但流利?
<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>
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用'参考',但我不需要相关表格中的所有列.我只需要一个房产.
gil*_*lyb 25
帕克说的不对.这可以在Fluent NHibernate中完成.我在网上搜索了很长一段时间,找不到任何人谈论这个选项,所以我只是玩了一点FNHibernate并最终设法做到了.
这是我的情景:
我有两张桌子 -
"FormFields" => Columns { "FieldId", "FieldName", "FieldType", "DisplayOrder" }
"FormStructure" => Columns { "FormId", "FormType", "FieldId" }
Run Code Online (Sandbox Code Playgroud)
这些是我的实体:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我的查询中有几个返回FormStructure对象列表的方法.我希望这些方法能够让我DisplayOrder按照FormField对象中的字段对它们进行排序,并希望在其对象中也DisplayOrder可以将其作为属性FormStructure用于其他原因.
这基本上意味着我需要连接表,以便我从FormStructure表中检索所有列以及表中的DisplayOrder列FormField,并将它们连接到匹配的FieldId列上.
我做了什么来解决这个问题:
我在我的FormStructure对象中添加了一个名为DisplayOrder的属性.
public virtual int? DisplayOrder { get; set; }
Run Code Online (Sandbox Code Playgroud)我将Join方法添加到我的FormStructure映射类中,所以它看起来像这样.
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();
});
}
}
Run Code Online (Sandbox Code Playgroud)该Join方法显然会在您在Join中的KeyColumn方法中定义的列上的两个表之间进行连接.
这也将删除一些具有空值的行.为了避免这种情况(我最近遇到过此问题),您可以m.Optional();在Join方法内添加.
我现在可以检索FormStructure对象列表,按顺序排序DisplayOrder,甚至DisplayOrder可以作为FormStructure对象中的属性.
return session.CreateCriteria<FormStructure>()
.Add(Expression.Eq("FieldName", fieldName))
.AddOrder(Order.Asc("DisplayOrder"))
.List<FormStructure>();
Run Code Online (Sandbox Code Playgroud)
以前不可能这样做,因为它不会识别DisplayOrder我在那里的Order子句中的列.
| 归档时间: |
|
| 查看次数: |
19272 次 |
| 最近记录: |