Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

LINQ delete

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Poly - 19 Mar 2008 11:40 GMT
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
Jon Skeet [C# MVP] - 19 Mar 2008 11:46 GMT
> I have a problem.
> I am trying to delete an object like this
[quoted text clipped - 14 lines]
>
> 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.

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

Poly - 19 Mar 2008 11:55 GMT
>> I have a problem.
>> I am trying to delete an object like this
[quoted text clipped - 17 lines]
> 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?

Signature

___________
best regards
Poly

Jon Skeet [C# MVP] - 19 Mar 2008 12:00 GMT
<snip>

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

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

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

Poly - 19 Mar 2008 12:09 GMT
> <snip>
>
[quoted text clipped - 4 lines]
>
> 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.

Signature

___________
best regards
Poly

Jon Skeet [C# MVP] - 19 Mar 2008 12:16 GMT
> >>> In my experience with other ORMs, if anything fails at the database
> >>> it's best to start with a fresh context anyway.
[quoted text clipped - 6 lines]
> 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?

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

Poly - 19 Mar 2008 12:21 GMT
> 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.

Signature

___________
best regards
Poly

Jon Skeet [C# MVP] - 19 Mar 2008 12:35 GMT
> > 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.

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

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

Frans Bouma [C# MVP] - 20 Mar 2008 09:55 GMT
> > 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.

    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

Signature

------------------------------------------------------------------------
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 - 20 Mar 2008 12:12 GMT
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.

Signature

/\/\arkus.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.