Steve Steve - 1 year ago 56
C# Question

ObjectContext.SaveChanges() never returns if new object added to an ObjectSet. Has anyone experienced this?

I have an EF4 EDM called

. The database is stored in SQL Express 2012. Windows 7 64-bit.

  1. I create a new context with
    context = new UnitHistoryEntities()

  2. I pass this context into a couple dialog boxes that collect a bit of information from the user. A few queries were probably performed during this.

  3. Based on that input, I construct a new Unit entity with
    var unit = new Unit()
    , set a few properties, and add it to the context with

  4. I then call

The call to
never, ever returns (I've given it at least 10 minutes) and never throws an exception. If I remove the
, it returns without making any changes to the database, because nothing was modified.

Why does
never return? Anyone experience this phenomenon?

I am able to connect and perform queries on the database from SQL Management Studio while my program is in this condition.

Answer Source

Turns out the problem I'm having is very similar to this question. I have a DateTime field that I was not setting. I made a console app that performed exactly the same actions on the context that my application was doing. In this case, SaveChanges() did return, with the exception The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.

So I now set my DateTime field to 1/1/1900 and I'll treat that like a null date. My program now operates properly.

Strange that I didn't get an exception thrown in my WPF application, but I did in the console application...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download