Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to SQL Foreign Keys & Collections
    primarykey
    data
    text
    <p>I'm a still trying to wrap myself around LINQ to SQL and foreign key relationships.</p> <p>I have a table called "Parent" and another called "Children". I have a OneToMany relationship, where a Parent can have multiple Children. My DBML looks something like:</p> <pre><code>&lt;Table Name="" Member="Parents"&gt; &lt;Type Name="Parent"&gt; &lt;Column Member="ParentID" Type="System.String" IsPrimaryKey="true" CanBeNull="false" /&gt; &lt;Column Member="ChildID" Type="System.String" CanBeNull="false" /&gt; &lt;Association Name="Parent_Child" Member="Childs" ThisKey="ParentID" OtherKey="ParentID" Type="Child" /&gt; &lt;/Type&gt; &lt;/Table&gt; &lt;Table Name="" Member="Childs"&gt; &lt;Type Name="Child"&gt; &lt;Column Member="ChildID" Type="System.String" IsPrimaryKey="true" CanBeNull="false" /&gt; &lt;Column Member="ParentID" Type="System.String" CanBeNull="false" /&gt; &lt;Association Name="Parent_Child" Member="Parent" ThisKey="ParentID" OtherKey="ParentID" Type="Parent" IsForeignKey="true" /&gt; &lt;/Type&gt; &lt;/Table&gt; </code></pre> <p>In my code, I would like do to something like:</p> <pre><code>// parent has already been loaded from the datacontext parent.Childs = &lt;some collection of children&gt; db.SubmitChanges(); </code></pre> <p>But when I do that I get the following error: </p> <p><em>A member defining the identity of the object cannot be changed. Consider adding a new object with new identity and deleting the existing one instead.</em></p> <p>Can anyone tell me how to properly accomplish 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.
    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