[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 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.

你可能感兴趣的:(C#)