我发现NHibernate在一些选择之后会做出意想不到的更新。
public class BasePriceRule : BaseEntity
{
public virtual string Name { get; set; }
public virtual string RuleString { get; set; }
public virtual TypePriceRule Type { get; }
public virtual DateTime Date { get; set; } = DateTime.Now;
}
public class ProductGroupRule : BasePriceRule
{
public virtual ProductGroup ProductGroup { get; set; }
public override TypePriceRule Type => TypePriceRule.ProductGroup;
}
public class ProductGroup : BaseEntity
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual UoM UoM { get; set; }
public virtual CustomProductType Type { get; set; }
}
public class BasePriceRuleMap : ClassMap<BasePriceRule>
{
public BasePriceRuleMap()
{
Table("PriceRule");
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.RuleString).Length(4096);
DiscriminateSubClassesOnColumn("Type");
Map(x => x.Type).CustomType<TypePriceRule>().ReadOnly().Access.None().Not.Nullable();
Map(x => x.Date).Not.Nullable().Default("CURRENT_TIMESTAMP");
}
}
public ProductGroupRuleMap()
{
KeyColumn("Id");
References(x => x.ProductGroup).Fetch.Join()/*.Not.Update()*/;
DiscriminatorValue((int)TypePriceRule.ProductGroup);
}
public ProductGroupMap()
{
Id(x => x.Id);
Map(x => x.Code).Unique();
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.Type).CustomType<int>();
References(x => x.UoM);
}
所以当我运行这个最简单的查询时:
_session.QueryOver<ProductGroupRule>();
或者这个:
_session.QueryOver<ProductGroupRule>().Fetch(x => x.ProductGroup).Eager.List();
我收到这个生成的sql:
2016-11-18 17:17:39.3103 NHibernate.SQL SELECT this_.Id as Id62_1_, this_.Name as Name62_1_, this_.RuleString as RuleString62_1_, this_.Type as Type62_1_, this_.ProductGroup_id as ProductG6_62_1_, productgro2_.Id as Id66_0_, productgro2_.Code as Code66_0_, productgro2_.Name as Name66_0_, productgro2_.Description as Descript4_66_0_, productgro2_.Type as Type66_0_, productgro2_.UoM_id as UoM6_66_0_ FROM PriceRule this_ left outer join [ProductGroup] productgro2_ on this_.ProductGroup_id=productgro2_.Id WHERE this_.Type='2'
2016-11-18 17:17:39.4474 NHibernate.SQL Batch commands:
command 0:UPDATE [ProductGroup] SET Code = @p0, Name = @p1, Description = @p2, Type = @p3, UoM_id = @p4 WHERE Id = @p5;
command 1:UPDATE [ProductGroup] SET Code = @p0, Name = @p1, Description = @p2, Type = @p3, UoM_id = @p4 WHERE Id = @p5;
--Numbers of commands are equal to the amount of records in PriceRule table
我试过了:
但仍然无法摆脱它们。
有人可以解释为什么会出现这些更新命令吗?
答案 0 :(得分:2)
在ProductGroup
定义中,有一个属性,不能 null - CustomProductType Type
public class ProductGroup : BaseEntity
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual UoM UoM { get; set; }
// this cannot be null
public virtual CustomProductType Type { get; set; }
}
C#会将其设置为默认值(通常是第一个enum
)
但似乎在DB中,相关列包含null。对于NHibernate,它是一个符号:1)loaded是NULL,但是在检查对象时 - 2)它有一个赋值。
所以,对象很脏,因为Flush模式默认是AUTO ...它试图保持DB和C#实例同步
只需更改映射
即可 public virtual CustomProductType? Type { get; set; }
答案 1 :(得分:0)
我的猜测是发生了自动冲洗,NH认为那些ProductGroup
对象由于某种原因是脏的。恕我直言,最好的行动方案是enable logging通过log4net获取一些详细的诊断信息。 NH的伐木非常好。你应该能够通过它来了解这一点。