我有一个有两个计算列的类.公式是从其他表中获取计数的select语句,如下所示:
private const string VOTES_FORMULA = "(select count(v.id) from Votes v where v.BusinessID = Id)";
private const string SURVEY_FORMULA = "(select cast((case when exists (select * from surveys s where s.businessid = Id) then 1 else 0 end) as bit))";
// in my bootstrap code...
mappings.Override<Business>(map =>
{
map.IgnoreProperty(x => x.IsNewRecord);
map.IgnoreProperty(x => x.IdString);
map.Map(x => x.UserPassword).CustomType<EncryptedStringType>();
map.Map(x => x.HasTakenSurvey).Formula(SURVEY_FORMULA).Not.Insert().Not.Update();
map.Map(x => x.Votes).Formula(VOTES_FORMULA).Not.Insert().Not.Update();
});
这一切都与Fluent NHibernate 1.1(使用NHibernate 2.1)完美配合,但我刚升级到1.2(使用NH 3.1),看起来Fluent NHibernate忽略了公式.我得到两个字段HasTakenSurvey和Votes的“无效列名”例外,因为它’试图直接查询列而不是按照指示执行公式.一个示例查询:
exec sp_executesql N选择TOP(@ p0)business0_.Id为Id0_,business0_.UserPassword为UserPass2_0_,business0_.HasTakenSurvey为HasTaken3_0_,business0_.投票为Votes0_,business0_.Origin为Origin0_,business0_.SecurityToken为Security6_0_,business0_.BusinessName为Business7_0_,business0_.BusinessType为Business8_0_,business0_.BusinessImageUrl为Business9_0_,business0_.BusinessDescription为Busines10_0_,business0_.EmployeeCount为Employe11_0_,business0_.OwnerFirstName为OwnerFi12_0_,business0_.OwnerLastName为OwnerLa13_0_,business0_.UserPosition为UserPos14_0_,business0_.BusinessAddress1为Busines15_0_, business0_.BusinessAddress2为Busines16_0_,business0_.BusinessCity为Busines17_0_,business0_.BusinessState为Busines18_0_,business0_.BusinessPostal为Busines19_0_,business0_.BusinessCountry为Busines20_0_,business0_.UserBusinessPhone为UserBus21_0_,business0_.UserMobilePhone为UserMob22_0_,business0_.UserEmailAddress为UserEma23_0_,business0_.用户IpAddress为UserIpA24_0_,business0_.OptInReminders为OptInRe25_0_,business0_.OptInReminders为OptInOf26_0_,business0_.OptInSms为OptInSms0_,business0_.Created为Created0_,business0_.Modified为Modified0_ from dbo.Businesses business0_ order by business0_.BusinessName asc’,N’@ p0 INT”,@ P0 = 25
实施是否改变了?我究竟做错了什么?
最佳答案 正如注释中所述,ConventionBuilder.Property.Always(x => x.Column(x.Property.Name))将列添加到所有属性(并覆盖公式).
将.Columns.Clear()添加到映射应删除列,因此:
mappings.Override<Business>(map =>
{
map.IgnoreProperty(x => x.IsNewRecord);
map.IgnoreProperty(x => x.IdString);
map.Map(x => x.UserPassword).CustomType<EncryptedStringType>();
map.Map(x => x.HasTakenSurvey).Formula(SURVEY_FORMULA).Not.Insert().Not.Update().Columns.Clear();
map.Map(x => x.Votes).Formula(VOTES_FORMULA).Not.Insert().Not.Update().Columns.Clear();
});