Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating LINQ to SQL object causing System.NotSupportedException
    primarykey
    data
    text
    <p>I get <code>System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new perhaps having been loaded from another DataContext</code> when I want to update an object's with child entities.</p> <p>The scenario is like this:</p> <p>I have a SubscriberProvider that allows me to create subscribers.</p> <pre><code>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); </code></pre> <p>On a normal subscribe page, this works fine.</p> <p>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:</p> <pre><code>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 </code></pre> <p>The exception occurs when SubscriberProvider tries to add interests of the member. Commenting out</p> <pre><code>newSubscriber.Interests.Add(new Interest{ Id=1, Name="cars" }); </code></pre> <p>will make member.Subscribe() work.</p> <p>member.Subscribe() is simply:</p> <pre><code> public void Subscribe(bool emailIsVerified, bool receiveEmails, bool sendDoubleOptIn) { var provider = new MailingListProvider(); provider.Subscribe(EmailAddress, emailIsVerified, receiveEmails, CountryId, sendDoubleOptIn, ConvertInterests(MemberInterests.ToList())); } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload