Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - Row not found or changed

I have the following code:

        Guid id = imageMetaData.ID;

        Data.LinqToSQL.Image dbImage = DBContext.Images.Where(x => x.ID == id).SingleOrDefault();

        dbImage.width = imageMetaData.Width;
        dbImage.height = imageMetaData.Height;

        DBContext.SubmitChanges();

Looking at SQL Profiler, the following SQL is being generated:

exec sp_executesql N'UPDATE [dbo].[Images]
SET [height] = @p0, [width] = @p1
WHERE 0 = 1',N'@p0 int,@p1 int',@p0=603,@p1=365

Why does my where statement not include a "where id=...."???

What am I doing wrong?

My application throws a ChangeConflictException with the message... "Row not found or changed"

like image 662
iasksillyquestions Avatar asked Feb 03 '09 21:02

iasksillyquestions


2 Answers

Check to make sure your model matches the DB. I've run across this if they are out of sync, in most cases the nullable flag. Check out this article for a possible cause.

I assume you are using the release version of .net 3.5 (Visual Studio 2008).

like image 70
Robert Wagner Avatar answered Oct 23 '22 21:10

Robert Wagner


Found the solution in: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/c672c8ee-bf2a-41b4-bb8b-aa76cc5d9b95 (posted by Todd Fleenor)

The problem can be solved this error by redragging the table from the server explorer to the designer and rebuilding. So the designer was out-of-synch with the SQL Table...

like image 44
Paulo J. Brito Avatar answered Oct 23 '22 22:10

Paulo J. Brito