[Troubleshooting] Nhibernate usage

1. NHibernate.PropertyValueException: not-null property references a nullor transient value ABC.DE.FG

Insert value does not set null

2. Row was updated or deleted by another transaction (orunsaved-value mapping was incorrect)

try
{
	Session.Persist(instance);
}
catch (PersistentObjectException e)
{
	try
	{
		Session.Merge(instance);
	}
	catch (StaleObjectStateException ex)
	{
		Session.Save(instance);
	}
}

3. NHibernate: SqlDateTime overflow. Must be between 1/1/175312:00:00 AM and 12/31/9999 11:59:59 PM.

Reason: System.DateTime is avalue type in .Net, which means that it can never be null. But what happenswhen you have a nullable date time in the database, and you load it into aDateTime type? When NHibernate loads a null value from the database, itcannot put a null in the UpdatedDate property, the CLR doesn't allow it. Whathappens is that the UpdatedDate property is set to the default DateTime value,in this case: 01/01/0001.

Solution:

public virtual DateTime UTCCreateDt { get; set; }

->public virtual DateTime? UTCCreateDt { get; set; }

->public virtual Nullable<DateTime> UTCCreateDt {get; set; }

4.  FluentNHibernate.Cfg.FluentConfigurationException:An invalid or incomplete configuration was used while creating aSessionFactory. Check PotentialReasons collection, and InnerException for moredetail.

---> FluentNHibernate.Visitors.ValidationException: Theentity 'AccrualMethodModel' doesn't have anId mapped. Use the Id method to map your identity property. Forexample: 

Reason: Every mapping requires an Id of some kind. The Id ismapped using the Id method, which takes a lambda expression thataccesses the property on your entity that will be used for the Id. Depending onthe return type of the property accessed in the lambda, Fluent NHibernate willmake some assumptions about the kind of identifier you're using. For example,if your Id property is an int, an identity column is assumed. Similarly,if you use a Guid then a Guid Comb isassumed.

Id(x => x.Id);

Note that the property you supply to Id can haveany name; it does not have to be called "Id," as shown here.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值