Groups | Blog | Home
all groups > dotnet framework > march 2008 >

dotnet framework : LINQ delete



Jon Skeet [C# MVP]
3/19/2008 10:46:21 AM
[quoted text, click to view]

In my experience with other ORMs, if anything fails at the database
it's best to start with a fresh context anyway.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/19/2008 11:00:03 AM
[quoted text, click to view]

<snip>

[quoted text, click to view]

I don't know, I'm afraid - I'm far from an expert on LINQ to SQL.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/19/2008 11:16:08 AM
[quoted text, click to view]

What's your design here? In my previous experience, I've written web
services using ORMs - and the whole service call fails if any database
call fails (and any existing transactions are aborted).

Do you really have to keep going with your half-working data?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/19/2008 11:35:16 AM
[quoted text, click to view]

In that case, I'm afraid you need someone with more LINQ to SQL
experience than me :(

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Poly
3/19/2008 1:40:25 PM
I have a problem.
I am trying to delete an object like this
try {
dataClasses1.myobjetcs.DeleteOnSubmit(obj);
dataClasses1.SubmitChanges();
}
catch {
MessageBox.Show("нельзя удалить");
}

And if delete fails, i can not work with my database any more, because
on any dataClasses1.SubmitChanges() called, i have an error.
I have tryed to refresh obj state like this

dataClasses1.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues,
obj);

but it don't helps.



best regards
Poly
3/19/2008 1:55:40 PM
[quoted text, click to view]

thnks, i thought about it.
but is there only way?

--
___________
best regards
Poly
3/19/2008 2:09:11 PM
[quoted text, click to view]

I tried to create new datacontext but i cant make this way =(. I have
lots of objects traked datacontext and if i reload datacontext all
tracking failed.

--
___________
best regards
Poly
3/19/2008 2:21:50 PM
[quoted text, click to view]

If i can not delete an item i need only to show message about it and to
continue working.


--
___________
best regards
Frans Bouma [C# MVP]
3/20/2008 1:55:17 AM
[quoted text, click to view]

The problem with Linq to Sql's context is that it is an entire unit
of work. This means that all entities which are 'dirty' (== changed or
new), or marked for deletion will be persisted in 1 transaction when
you say SubmitChanges. If one of those entities fails, you'll run into
the problem that the whole transaction has to roll back.

With the GetChangeSet method you get the list of entities which are
dirty or otherwise involved in the transaction.

I don't know if Linq to Sql throws an exception which contains the
entity which was involved, but the exception might be helpful in fixing
the issue.

The entity which caused the transaction to fail has to be removed from
the context, otherwise you'll never make it work.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
Markus Springweiler
3/20/2008 12:12:35 PM
Frans,

[quoted text, click to view]

When you use
SubmitChanges(ConflictMode.ContinueOnConflict);
then you can query all conflicts via the DataContext property
"ChangeConflicts".

[quoted text, click to view]

Each member of the ChangeConflicts collection (type ObjectChangeConflict)
contains lots of data why it failed and also a method "Resolve" for
reloading from database and an overload which helps with rows which where
deleted in the database meanwhile.

--
AddThis Social Bookmark Button