Linq to SQL select and update oddity

by Mark Wiseman on April 24th, 2009 | Posted in Revium Sandbox | Comment on this entry

Today i came across some very odd behaviour using Linq to SQL.
I was trying to retrieve a record from the database to update one column then save the change to the database. The problem was happening when i tried to save the change. I was getting foreign constraint errors on related records that didn’t even exist!

Here is the DB structure:
T1 (id, value)
T2 (id, T1.id, values)
T3 (id, T2.id, values)

T1 existed but T2 and T3 didn’t. The foreign key constraint error i recieved was between T2 and T3 but how can that be when i didn’t create any records and they don’t exist?!

Here is the code i initially used:

DataContext Dal = new DataContext();

T1 table1 =  (from t in DC.T1
    where t.id == pID
    select t).SingleOrDefault();

table1.value = pNewValue;
Dal.SubmitChanges();

When i inspected the object VS showed that T1 had at least 1 T2 object and when i inspected T2 it had at least one T3 object.

The fix for this was to change the Linq select statement but i don’t for the life of me know why this made a difference. Here is the fixed version:

DataContext Dal = new DataContext();

T1 table1 =  Dal.T1.Where(a => a.id == pID).SingleOrDefault();

table1.value = pNewValue;
Dal.SubmitChanges();

Has anyone else ever experienced this?

Related posts:

  1. Limiting LINQ String Field Lengths
  2. Linq – SubmitChanges does not work

Tags: linq

« ASP.NET Button – prevent double clicks

Power of the ASP.NET MVC + jquery »

Leave a Reply

Click here to cancel reply.

Recent Articles

  • 10 Tips for user acceptance testing of web applications
  • Popular Content Management Systems
  • ASP.NET MVC [HandleError] and logging
  • VMWare hard lessons
  • Linq – SubmitChanges does not work
  • Using FourSquare for Marketing
  • ASP.NET Button – prevent double clicks part 2
  • The easy way to clear floated elements

Twitter

  • 50 Useful Tools & Resources For Web Designers <http://www.smashingmagazine.com/2010/07/26/50-useful-tools-and-resources-for-web-designers/> 3 days ago
  • Tourism Australia has launched a free "Go Ashes" app for iPhone to promote England vs Australia cricket series 4 days ago
  • #YouTube competition, on 24 July capture you Life In A Day http://youtu.be/tZFbDY3-eG4 1 week ago
  • More updates...

Revium Logo

  • Home
  • About
  • Expertise
  • Showcase
  • Contact

  • news
  • blog
  • sandbox
  • twitter
  • facebook
  • rss
Bookmark and Share

We are Revium, hear us roar!

The news.

23 Apr

Revium sponsors IIA CEO luncheon on 2010 Challenges

Last Wednesday 14th April Revium hosted a CEO luncheon for the Internet Industry Association (IIA). Revium have been long time supporters of the IIA and…

Continue reading
View archive

The blog.

11 Jun

VMWare hard lessons

Well, I learned a hard lesson today when I decided to give more hard drive space to one of our virtual machines. We run…

Continue reading
View archive

The sandbox.

02 Jul

10 Tips for user acceptance testing of web applications

1. Draw up a testplan Many testers would like to skip this step, but it is essential for structured testing. It depends of course on the…

Continue reading
View archive

eNewsletter.

 

© Copyright 2010. All Rights Reserved.

Revium Pty Ltd

e / info@revium.com.au t / Work +61 3 9429 2000

Find us: web development, seo

Disclaimer and privacy Revium Pty Ltd

10 Harvey Street
Richmond, Victoria, 3121 Australia
View map

Logo Partner MicrosoftLogo Partner KenticoLogo Partner IiaLogo Partner Iia RibLogo Partner Acs