<snip>
> Category c1 = new Category();
> c1.CategoryID = 1;
[quoted text clipped - 5 lines]
> c.CategoryID==1 select c).Single();
> Assert.AreEqual(c1.CategoryName, c2.CategoryName);
Right, that's more like it. Is this your *actual* code, or do you have
more code around there?
> The categoryName is not updated - why?
Well, you've told it to insert, not to update. Is there already a
Category with ID 1 before you run this code? Does it run without any
exceptions?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Ilyas - 17 Mar 2008 15:40 GMT
> <snip>
>
[quoted text clipped - 20 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk
I do apologise for the confusion.
Im still confused. What im trying to do is to update an object without
getting linq to fetch it first
//Get the category t update
Category c1 = new Category();
c1.CategoryID = 1;
//Attach the object
nw1.Categories.Attach(c1);
//Now update the category
c1.CategoryName = "New Category2";
//save changes
nw1.SubmitChanges();
This gives me the error ChangeConflictException "Row not found or
changed" - my issue is that im not sure why this happens?
Jon Skeet [C# MVP] - 17 Mar 2008 15:46 GMT
> I do apologise for the confusion.
> Im still confused. What im trying to do is to update an object without
> getting linq to fetch it first
Hmm... not sure about that.
> //Get the category t update
> Category c1 = new Category();
[quoted text clipped - 11 lines]
> This gives me the error ChangeConflictException "Row not found or
> changed" - my issue is that im not sure why this happens?
I suspect it's due to how you've got change tracking configured. As
ever, set the log to write the SQL out where you can see it - that's
likely to give hints as to what's going on.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Marc Gravell - 17 Mar 2008 16:29 GMT
Without a timestamp column it issues a WHERE matching all the old values -
i.e. (from a simple PUBS example):
UPDATE [dbo].[Employees]
SET [Extension] = @p16
WHERE ([EmployeeID] = @p0) AND ([LastName] = @p1) AND ([FirstName] = @p2)
AND ([
Title] = @p3) AND ([TitleOfCourtesy] = @p4) AND ([BirthDate] = @p5) AND
([HireDa
te] = @p6) AND ([Address] = @p7) AND ([City] = @p8) AND ([Region] = @p9) AND
([P
ostalCode] = @p10) AND ([Country] = @p11) AND ([HomePhone] = @p12) AND
([Extensi
on] = @p13) AND ([ReportsTo] = @p14) AND ([PhotoPath] = @p15)
Without the necessary data, it somehow short-circuits to "0=1". I haven't
checked how it detects this.
I think you will struggle to UPDATE an object that you didn't get via LINQ.
You can probably do it if you try hard enough... but... why? Note that you
can serialize/deserialize to round-trip:
Employee emp1;
using (PubsDataContext ctx = new PubsDataContext())
{
ctx.Log = Console.Out;
Employee emp0 = ctx.Employees
.Where(x => x.EmployeeID == 1).Single();
// make a seraialized clone
DataContractSerializer dcs = new
DataContractSerializer(typeof(Employee));
using(MemoryStream ms = new MemoryStream()) {
dcs.WriteObject(ms, emp0);
ms.Position = 0;
emp1 = (Employee) dcs.ReadObject(ms);
}
}
using (PubsDataContext ctx = new PubsDataContext())
{
ctx.Log = Console.Out;
ctx.Employees.Attach(emp1);
emp1.Extension = "2314";
ctx.SubmitChanges();
}
Ilyas - 17 Mar 2008 18:02 GMT
> Without a timestamp column it issues a WHERE matching all the old values -
> i.e. (from a simple PUBS example):
[quoted text clipped - 40 lines]
> ctx.SubmitChanges();
> }
This doesnt make any sense to me. In effect what you are saying is
that I need to fetch the objec from linq first, make the changes and
then update it
My question is why do I need to get it from Linq first?
I've noticed that the update command issued is
exec sp_executesql N'UPDATE [dbo].[Categories]
SET [CategoryName] = @p0
WHERE 0 = 1',N'@p0 nvarchar(13)',@p0=N'New Category2'
..which of course wont actually update any rows. Interesting enough
theres no mention of my CategoryId...
Jon Skeet [C# MVP] - 17 Mar 2008 20:31 GMT
<snip>
> This doesnt make any sense to me. In effect what you are saying is
> that I need to fetch the objec from linq first, make the changes and
> then update it
Or change the change-tracking method your entity uses.
> My question is why do I need to get it from Linq first?
LINQ is trying to make sure you're not overwriting someone else's
changes accidentally.
> I've noticed that the update command issued is
> exec sp_executesql N'UPDATE [dbo].[Categories]
[quoted text clipped - 3 lines]
> ..which of course wont actually update any rows. Interesting enough
> theres no mention of my CategoryId...
That's exactly what Mark said:
<quote>
Without the necessary data, it somehow short-circuits to "0=1". I
haven't checked how it detects this.
</quote>

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk