The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

sammisoultan (4) [Avatar] Offline
#1
Hello,

I'm experiencing some update-related problems in the following scenario: I have two data contexts connecting to exactly the same database.
After creating them, I am retrieving an entity using the first data context, changing one of its properties, the calling SubmitChanges() in this first datacontext. Now if I look into the database, the object has not been updated.

The same, if aterwards I am trying to retrieve the entity from the database using the second data context, I will still see that the object has not been updated.

Here's an excerpt from the code:

ModelDataContext context1=new ModelDataContext(serverConnString, mapping);
ModelDataContext context2=new ModelDataContext(serverConnString, mapping);

BaseEntity e=context1.BaseEntities.SingleOrDefault(l => l.Name == "entity1"smilie;
//at this point, e.Name would be equal to "entity1"

e.Name = "some_name";
context1.SubmitChanges();
context1.Refresh(RefreshMode.OverwriteCurrentValues, e);
//here, if I test e.Name it would still be "entity1"

I have specified that I am using two datacontexts just to help you with extra info, maybe it might indicate what the problem is.

Can anyone please help me understand why the value of "e" is not updated in the DB?

Tnank you,
Sammi.
fabrice.marguerie (224) [Avatar] Offline
#2
Re: Update problems with SubmitChanges
Hello Sammi,

Does it happen only with this DataContext and mapping?
Does it happen only with the BaseEntity type?
Does it happen only for the Name property?
Is Name the primary key? What's the primary key?

Fabrice
sammisoultan (4) [Avatar] Offline
#3
Re: Update problems with SubmitChanges
Hi Fabrice, and thank you for your reply.

I haven't tested it on another data context + mapping. It does not happen only with this type of entity, and also I've tried modifying other properties as well. The 'Name' property is not a primary key - I would get an error if I'd try to update the PK.

I've used the SQL Server Profiler to see what commands are reaching the server on SubmitChanges : on SubmitChanges I see that no command ever reaches the server. The weirdest thing is that I receive no error, which is extremely odd.

What other thing should I try/check?
sammisoultan (4) [Avatar] Offline
#4
Re: Update problems with SubmitChanges
Correction brought to my last reply:

I must appologize, I've tested it deeper, and I concluded it only happens with this entity type (BaseEntity).
This entity is the root of a hierarchy of objects, binded together via associations. All other objects from lower hierarchy levels can be updated with code like the one described in my first post. BaseEntity however cannot be.

Thanks, waiting to hear from you.
Sammi
fabrice.marguerie (224) [Avatar] Offline
#5
Re: Update problems with SubmitChanges
It may well be a weakness of LINQ to SQL. Maybe the tracking of updates applies only to the lowest hierarchy levels?
Does your BaseEntity class have a Table attribute? The Name property has a Column attribute, right?

Fabrice
sammisoultan (4) [Avatar] Offline
#6
Re: Update problems with SubmitChanges
Yes, BaseEntity has a Table attribute, and you're also right about the Name property.

When I query the first time to get the object (.SingleOrDefault(..)), shouldn't the context automatically start tracking the object? I mean, right before executing SubmitChanges(), I checked the list of objects received with context1.GetChangeSet(), and there is indeed 1 update - the object 'e' with its Name property modified. Can I take a glimpse in some way at the tracking information?

Sammi.
jwooley (123) [Avatar] Offline
#7
Re: Update problems with SubmitChanges
This is unusual behavior indeed. Would it be possible to send me a sample application to take a look at (info AT thinqlinq DOT com)? Since it is showing in the GetChangeSet, it should be updating. You may want to check the MSDN forums as well as I recall a similar situation being reported there some time ago. The forums are at http://forums.microsoft.com/Forums/ShowForum.aspx?siteid=1&ForumID=123.

Jim
fabrice.marguerie (224) [Avatar] Offline
#8
Re: Update problems with SubmitChanges
Anyone searching for a solution to this problem should take a look at this post.