Tutorial :Updating LINQ to SQL object causing System.NotSupportedException



Question:

I get System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new perhaps having been loaded from another DataContext when I want to update an object's with child entities.

The scenario is like this:

I have a SubscriberProvider that allows me to create subscribers.

var provider = new SubscriberProvider(); // Creates a new repository with own datacontext    var newSubscriber = new Subscriber  {     EmailAddress = emailAddress,  };    newSubscriber.Interests.Add(new Interest{                                Id=1,                                Name="cars"                              });    provider.Subscribe(newSubscriber);  

On a normal subscribe page, this works fine.

Now I have a linq2sql Member class(retrievable by a MemberRepository) and I want to extend it to have a helper subscribe method like so:

var repository = new MembershipRepository(); // Holds its own datacontext  var member = repository.Get("member1");    member.Subscribe(); // transfer member's info and interests to subscriber's table  

The exception occurs when SubscriberProvider tries to add interests of the member. Commenting out

newSubscriber.Interests.Add(new Interest{                                Id=1,                                Name="cars"                              });  

will make member.Subscribe() work.

member.Subscribe() is simply:

    public void Subscribe(bool emailIsVerified, bool receiveEmails, bool sendDoubleOptIn)      {          var provider = new MailingListProvider();            provider.Subscribe(EmailAddress, emailIsVerified, receiveEmails, CountryId, sendDoubleOptIn, ConvertInterests(MemberInterests.ToList()));        }  

So what's causing the child entities(Interests) to lose their datacontext when I do member.Subscribe() and how do I go about fixing this?


Solution:1

It seems there's some code missing here, but I'll take a stab anyway because I think I have an idea what's going on.

If you have a different DataContext created for your MembershipRepository and your SubscriberRepository you're going to have issues related to entities "having been loaded from another DataContext." (as the Exception you posted points out). You can't just take an object out of one DataContext and save it into another.

It seems that you might have an architectural issue here. Should these 2 repositories actually be separate? If so, should they have completely different DataContexts? I would probably recommend using Dependency Injection to inject your DataContexts into your Repositories. Then you can decide how to cache your DataContexts.


Solution:2

That line of code you commented out is being flagged by the DataContext as a new record, even though it's likely that the record already exists, due to the error message.

Change the line to:

newSubscriber.Interests.Add(DataContext.Interests.Where(a => a.Id == 1).Single());  

Now, the DataContext will know that record is one that already exists, and won't try to add it as an Insert to the ChangeSet.


Solution:3

Found the solution to this myself. Turns out it was the ConvertInterests() method causing it. The converted interest object had an invalid declaration which compiled ok.

Thinking the code was simple enough, I didn't create a test for it. I should have known better!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »