LINQ delete

P

Poly

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
 
J

Jon Skeet [C# MVP]

Poly said:
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.

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

Poly

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

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

Poly

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

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.
 
J

Jon Skeet [C# MVP]

Poly said:
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.

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?
 
P

Poly

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

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

Jon Skeet [C# MVP]

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

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

Frans Bouma [C# MVP]

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

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#)
------------------------------------------------------------------------
 
M

Markus Springweiler

Frans,
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.

When you use
SubmitChanges(ConflictMode.ContinueOnConflict);
then you can query all conflicts via the DataContext property
"ChangeConflicts".
The entity which caused the transaction to fail has to be removed from
the context, otherwise you'll never make it work.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top