我在使用 FluentNhibernate 公式映射时遇到问题。我需要在公式中使用连接表中的列。
问题在以下三个表中得到演示:城市、人员、地址
class Person {
int PersonId { get; set; }
int AddressId { get; set; }
Address PersonAddress { get; set; }
string CityName { get; set; }
}
class Address {
int AddressId { get; set; }
string Street { get; set; }
}
class AddressMap<Address> {
Id(x => x.AddressId, "ADDRESS_ID");
Map(x => x.Street, "STREET");
}
class PersonMap<Person> {
Id(x => x.Id, "PERSON_ID");
References(x => x.PersonAddress).Column("ADDRESS_ID);
Map(x => x.CityName).Formula("select Name from City c where c.street = STREET");
// Doesn't work, STREET is a part of the joined table !
}
知道如何正确编写公式映射吗?当我使用值重写映射时 NHibernate 将生成,一切都会工作,尽管如此,这是一个非常肮脏的解决方案:
Map(x => x.CityName).Formula("select Name from City c where c.street = address1_.STREET");
// Works !!
非常感谢任何帮助!
最佳答案
NHibernate 中没有直接的方法来支持另一个联接表的别名。为什么?
because that joined table simply does not have to be part of the join
考虑一下延迟加载的地址 - 即不是 SELECT 的一部分,或者一些没有地址表的投影。
公式旨在作为一种智能或更智能方式来访问当前表中的数据。或者如何创建一些独立的子选择、子查询..并传递当前行id
作为引用过滤器。
例如在这里,在 Ayende 的 NHibernate property Mapping ,我们可以看到:
<property name="CountOfPosts"
formula="(select count(*) from Posts where Posts.Id = Id)"/>
像这样生成 SELECT:
SELECT ...
// the injected 'Id' is from current table
(select count(*) from Posts where Posts.Id = this_.Id)
FROM [MainTable] this_ // the alias of current table
建议:City
或城市名称可以只是另一个引用(如果不是直接是地址的字符串属性)。它将允许我们非常轻松地使用它(选择、投影、过滤、排序),并且我们不会依赖于某些“隐藏的、硬编码的”映射。这将是干净的模型:
Person.Address.City.Name
关于c# - FluentNhibernate 公式与连接表中的参数进行映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23568195/