[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 theIdmethod, 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 anint, an identity column is assumed. Similarly,if you use aGuidthen aGuid Combisassumed.

Id(x => x.Id);

Note that the property you supply toIdcan haveany name; it doesnothave to be called "Id," as shown here.

,请让我们从容面对这离别之后的离别。

[Troubleshooting] Nhibernate usage

相关文章:

你感兴趣的文章:

标签云: