weblivz

One of the frustrations I am having right now is that i am updating a few objects in one method using the same entity connection. So i make the first update (which actually inserts some data) and that returned object is then set on a property prior to the second update. Works fine.

However, one thing is that when it gets complex like this - and i have situations where this happens over a few objects - i often get DateTime issues on the database. Saying i can't insert NULLs which is fine - only i have no idea what objects, or part of, is causing the problem.

I debug and all looks fine, but the error message doesn't give me an idea of where it is having a problem. So i check all my top level objects which have already been inserted or loaded in that context - i haven't actually added anything new that HAS a datetime.

Will the next set of August updates be any better for diagnosing this king of stuff

It's a great piece of software - saving me a LOT of time.

cheers,

steven

http;//livz.org


Re: ADO.NET (Pre-release) DateTime, Updates and Errors

weblivz

Hmmm - i made made every DateTime column in my database nullable but i'm still getting "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM." which previously happened when i tried to insert into nullable columns.

I have no idea what column is causing this - i think i have set them all... due to key relations i'm not sure how deep the update goes.. anyone run into these kind of issues

Would be nice to see a hierarchy of objects that are being updated.

steven

http://livz.org





Re: ADO.NET (Pre-release) DateTime, Updates and Errors

Daniel Simmons - MSFT

Before we go any farther it would be helpful to know if we're talking about LINQ to SQL or the Entity Framework.






Re: ADO.NET (Pre-release) DateTime, Updates and Errors

weblivz

Hi Daniel - was kind of posting as i debugged Smile

I am using Entity SQL.

I noticed if i take the entities i have just saved within one context (all three happened with a single "using" statement) and then do my second update in a second context (whereby i again load the dependent objects through their ID's within the next context) and set them as properties and THEN save, then it works fine.

It's like it loses some of the information from the objects i updated previously in the first case.

steven

http://livz.org





Re: ADO.NET (Pre-release) DateTime, Updates and Errors

weblivz

Hi Daniel - solved this. It was a subtle way in which i was updating my entity. I was adding a relationship before my entity had been fully saved. I have it working.

There are two things through this experience however that i think would be useful.

1. Some debug info about where the update failed. Could we get an object or table name and/or property

2. The ability to change context. Well, I had updated under context A and so it assigned a change tracker. I simply wanted to use that instance in a new context (a foreign key relation) but couldn't. I'd like to be able to tell it to "drop" any change management and put that new object in the new context. (Can i do this already ). Ideally without re-loading from the database.

Thanks for getting back so quick on this.

steven

http://livz.org





Re: ADO.NET (Pre-release) DateTime, Updates and Errors

Daniel Simmons - MSFT

I'm glad you solved your problem, but I am a bit confused by the solution you came up with. It doesn't seem like you should have to fully save an entity before you add a relationship to it. That said, it's hard for me to diagnose very well given the narrow bandwidth this communication channel provides (without seeing your code & database & model it's hard to tell just what is going on).

We'll give some thought to ways to provide better info when updates fail--sometimes it can be fairly difficult to track these things down. One thing that sometimes helps is to use the sql profiler to track the exact sql queries which are being sent to your database. That might enable you to better track down just what's going on.

As far as using two contexts goes, while it is true that any one object instance can only be associated with one context at a time (if that weren't the case things could get very confusing very fast), it is possible to detach an entity from one context and attach it to another without requerying the database. The Detach method on the context will unhook an entity from that context so that it is standalone and disconnected from the object graph of any other entities in that context. You can then call the Attach method on the other context and pass in the object instance. The result of that will be that your object instance will be assocaited with the new context and in the unchanged state. Attach essentially says "trust me, this is what my database looks like right now." Hopefully that will work for your scenario. There are some limitations, though. For instance, if your object has been modified, then when you detach it from the first context you will lose the original values/change tracking info. Your object will still be modified, but reattaching it to the new context will cause that context to think those changes have already been applied to the database. So you need to be careful of the state of the objects when you are detaching and attaching.

- Danny






Re: ADO.NET (Pre-release) DateTime, Updates and Errors

Colin Meek - Microsoft

UpdateException includes a StateEntries property which enumerates the state manager entries contributing to the failure (there are usually one or two entries). This can help to narrow down the source of problems.

Note that SQL Server and the CLR DateTime types have different ranges. This is the likely root cause of the "SqlDateTime overload" error you were seeing. The default value for CLR DateTime is actually out of range in the store, which means you will see this error when saving changes to an entity with a DateTime property that has not been set.

You can use the Detach and Attach methods to move an entity from one context to another.

Thanks,

-Colin






Re: ADO.NET (Pre-release) DateTime, Updates and Errors

weblivz

Hi Danny. Yes - it is hard to diagnose like this (and hard to explain sometimes).

The reason is because i use Data Tranfer Objects to serialize my Entities across the wire. So i have EntityA and WCFEntityA - i needed to do this due to issues i ran into when serializing Entities across the wire. I discussed this with Brian here. I figured having my control over this way the way to go.

Now I got my problem because when i deserialize from my WCF to my Data Entities, i am affectively creating new objects (because the load never got them from the database). So say i have object A with new data, but it has a property B which is a reference to something in the database. Well, even through i can save an instance of A, when it tries to save B it will attempt to create a new reference and will fail as all it has is an "ID" - it doesn't have a valid "CreatedDate" in my case (remember this was a DTO not an object loaded from the data store). I have to first load B and attach it to A as an actual object loaded from the DB before i save. When i do this it works fine. That make any sense I may not be the optimal way, but i'm learning at least Smile

I will look into Detach - it may well be what i need.

steven

http://livz.org





Re: ADO.NET (Pre-release) DateTime, Updates and Errors

weblivz

Ah, UpdateException - i will check that out.

The date thing is unfortunate but understandable. Of all the errors i have got this happens the most by a distance.

Thanks Colin.

steven

http://livz.org