开发者

DataContext disposed before updating foreign key field

开发者 https://www.devze.com 2023-02-06 18:13 出处:网络
I\'m having some problems with DataContexts using linq to sql for an asp.net c# web application. I first had problems with exceptions being thrown as I didn\'t dispose of the DataContext, same errors

I'm having some problems with DataContexts using linq to sql for an asp.net c# web application.

I first had problems with exceptions being thrown as I didn't dispose of the DataContext, same errors as in this question. I was using a static DataContext which was probably why it wasn't disposed of properly, and after reading this and several other articles I wrapped all calls in using statements to be sure they'll get disposed of.

However, now I got problems when I need to update a foreign key as the DataContext has already been disposed of. I'm not sure why it's been disposed of already, and what the best practice would be to do in this scenario so any ideas would be greatly appreciated!

Short example here:

UPDATE: I think my example was too confusing when I tried to mak开发者_Go百科e it as short as possible so here's a longer and hopefully better example:

private static void SendTexts(List<TextAlert> TextQueue)
{
    using (THTDataContext db = new THTDataContext())
    {
        foreach (TextAlert text in TextQueue)
        {
            try
            {
                // do IntelliSMS stuff
                
                // set status to 'sent'
                text.Status = 1;

                db.SubmitChanges();
            }
            catch (IntelliSMSException ex)
            {
                // set status to 'failed'
                text.Status = 2;
                
                db.SubmitChanges();
            }
        }
    }
}

Thanks,

Annelie


You probably need to attach the incoming MyThing to your new context. Something like this might work:

private static void DoMyStuff(MyThing thing)
{
    using (MyDataContext db = new MyDataContext())
    {
       db.MyThings.Attach(thing);
       thing.Status = 1;
       db.SubmitChanges();
    }
}

Once it's attached, the context should be able to track changes to it and, on the Submit, store them.

If this doesn't resolve the "already disposed" exceptions, then you might try "detaching" the MyThing before disposing of the old context. Note that if you have to go this route, however, the lifecycle of your DataContext is probably wrong, as entities are not designed to move about between contexts in the same app domain.

If you're interested in understanding the attach/detach business a bit better, MSDN's Dinesh Kulkarni has a short blog post about it.


Yikes! If an exception was thrown while trying to update an entry in the database, do you really think it's a good idea in the exception handler to try and update an entry in the database?

As for what's going on here, what's the source of thing? Did it come from a query that was executed on another instance of MyDataContext? If so, that's your problem right there.

0

精彩评论

暂无评论...
验证码 换一张
取 消