Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13840

SqlDateTime overflow using NHibernate

I persist my objects using NHibernate in the database

the App object have a property defined:

public virtual DateTime ReleaseDate { get; set; }

in the mappingClass:

Map(x => x.ReleaseDate).Not.Nullable();

which in the sqlServer 2008 its dataType is dateTime and is not nullable.

for the first Time it saves to database with no error. but after updating app info I encounter SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

but the app release date is a valid dateTime : 2/16/2014 2:21:58 AM and it's not null.

so Im confused why this exception raise?

ist<App> apps = session.QueryOver<Data.Model.App>()
            .List()
            .ToList();
.
.
.
.
for (int i = 0; i < apps.Count(); i++)
        {
            App appWithOldInfo = apps[i];

                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        //updating app info
                        appWithOldInfo = UpdateAppInfo(appWithOldInfo, appWithNewInfo);

                        session.Update(appWithOldInfo);
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }

see the screenshots: enter image description here enter image description here enter image description here

Upvotes: 1

Views: 4143

Answers (2)

Rutul Patel
Rutul Patel

Reputation: 11

If you are facing this problem, here are the steps to resolve it:

  • If you are saving an instance of class A, it doesn't necessarily mean the issue lies within that class alone. Hibernate manages the session, which includes all entities used within that session. It's possible that one of the entities is causing the issue. Essentially, this error indicates that you are trying to pass a null value into a not-null date field.

Upvotes: 0

Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13840

thanks guys for your helpful comments.

The problem wast That I was fetching device object from DB which has a property LastActivityDate

List<Device> devices = session.QueryOver<Data.Model.Device>().List().ToList();

I had added this property to model after addding a device object with some info to the DB. this property was null, but I haden't defined LastActivityDate as a nullable property.

so this object was in the same session of app object. when I flushed the session, because LastActivityDate was null, SqlDateTime exception rised!

it's simple. but I spend hours to find it!!

Upvotes: 2

Related Questions