C# 在Nhibernate中选择后出现意外的批更新命令
我发现NHibernate在经过一些选择之后进行了意外的更新 实体: 映射: 我试过:C# 在Nhibernate中选择后出现意外的批更新命令,c#,sql,nhibernate,fluent-nhibernate,C#,Sql,Nhibernate,Fluent Nhibernate,我发现NHibernate在经过一些选择之后进行了意外的更新 实体: 映射: 我试过: 启用/禁用延迟加载 将级联更改为无,逐出 为ProductGroup引用设置Not.Update() 使用未来 但还是无法摆脱它们 有人能解释为什么会出现这些更新命令吗?我猜是自动刷新发生了,NH认为那些ProductGroup对象由于某种原因是脏的。IMHO的最佳做法是通过log4net获得一些详细的诊断。NH的日志记录非常好。您应该能够通过它来了解这一点。在ProductGroup定义中,有一个属性不
- 启用/禁用延迟加载
- 将级联更改为无,逐出
- 为ProductGroup引用设置Not.Update()
- 使用未来
有人能解释为什么会出现这些更新命令吗?我猜是自动刷新发生了,NH认为那些
ProductGroup
对象由于某种原因是脏的。IMHO的最佳做法是通过log4net获得一些详细的诊断。NH的日志记录非常好。您应该能够通过它来了解这一点。在ProductGroup
定义中,有一个属性不能为null-CustomProductType
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#将其设置为默认值(通常是第一个枚举
)
但在DB中,相关列似乎包含null。对于NHibernate,它是一个符号:1)loaded为NULL,但在检查对象时-2)它有一个赋值
因此,对象是脏的,因为刷新模式默认为自动。。。它试图使DB和C#实例保持同步
只需更改映射即可
public virtual CustomProductType? Type { get; set; }
这有点奇怪,因为实际上我的数据库中没有空类型的ProductGroups。无论如何,将属性设置为Nullable或相反,将映射配置更改为Not.Nullable remove update语句。谢谢这里有一个链接,指向ghostbuster的原始代码。这是一个查找此类问题的好工具,但不确定它是否将与当前版本的nhibernate一起运行。
_session.QueryOver<ProductGroupRule>();
_session.QueryOver<ProductGroupRule>().Fetch(x => x.ProductGroup).Eager.List();
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
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; }
}
public virtual CustomProductType? Type { get; set; }