2

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

Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
  • 3
    Are you sure that's the *only* date and time in your database table? Perhaps there's another one that you've forgotten about, which is defaulting to 1/1/0001? – Jon Skeet Mar 08 '14 at 17:22
  • yeah I'm sure. see updated post – Hashem Aboonajmi Mar 08 '14 at 17:35
  • I have another field of type `timeStamp` for tracking version changing of entity. – Hashem Aboonajmi Mar 08 '14 at 17:42
  • And what's the value of that? – Jon Skeet Mar 08 '14 at 17:46
  • value is exactly: `` I can't see the binary data – Hashem Aboonajmi Mar 08 '14 at 17:49
  • Well how are you setting it? Have you tried to reproduce this with a simpler table and a short but complete example you could post? – Jon Skeet Mar 08 '14 at 17:51
  • 1
    The answer here could be in a **different instance** having the default `DateTime` - beeing loaded by *Session*. The suspected to me could be `appWithNewInfo`. If this (other instance) is just loaded from DB, i.e. kept in a Session, during the `session.Flush()` - even this instance is persisted. – Radim Köhler Mar 08 '14 at 17:53
  • I get appWithNewInfo is an app object which I create using JSON data I get from apple serever. then I update appWithOldInfo with info in appWithNewInfo. even I delete updating app method. I think appWithOldInfo has detached from session. I added some code. – Hashem Aboonajmi Mar 08 '14 at 18:25
  • Is an appWithOldInfo already loaded in this session that has the same primarykey as the one you are trying to update? If so, you will see the model you expect in the debugger but the session will actually be trying to do stuff with the previously loaded model. – Eric Morris Mar 08 '14 at 22:32

1 Answers1

2

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!!

Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
  • 1
    So the issue is that another object is being added to the NHibernate session. On the flush (or in my case the commit) NHibernate throws up on itself if all the objects in session dont have all of the right data. To prevent this add a .readonly() extension to the Query<> or QueryOver<> that way NHibernate doesnt track it. In Hashems example he could have done a `List devices = session.QueryOver().Readonly().List().ToList()` – Colin Pear Oct 07 '15 at 18:03