<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Entity Framework Archives | DMC, Inc.</title>
	<atom:link href="https://static.dmcinfo.com/blog/tag/entity-framework/feed/index.xml" rel="self" type="application/rss+xml" />
	<link></link>
	<description></description>
	<lastBuildDate>Mon, 15 Jun 2026 17:32:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>

<image>
	<url>https://static.dmcinfo.com/wp-content/uploads/2025/04/site-icon-150x150.png</url>
	<title>Entity Framework Archives | DMC, Inc.</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Duplicating Related Entities using Entity Framework Core</title>
		<link>https://static.dmcinfo.com/blog/21028/duplicating-related-entities-using-entity-framework-core/</link>
		
		<dc:creator><![CDATA[DMC]]></dc:creator>
		<pubDate>Thu, 29 Aug 2019 13:22:51 +0000</pubDate>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[Web Application Development]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[How To]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/21028/duplicating-related-entities-using-entity-framework-core/</guid>

					<description><![CDATA[<p>This blog will show how to use Entity Framework Core to copy multiple related entities stored in a SQL database while maintaining their keyed relationships. While EF Core doesn’t offer a `Copy` method, there are simple ways to duplicate an entry in a database. However, database schemas often use multiple entities, linked by foreign keys, [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/21028/duplicating-related-entities-using-entity-framework-core/">Duplicating Related Entities using Entity Framework Core</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">This blog will show how to use <strong>Entity Framework Core</strong> to copy multiple related entities stored in a SQL database while maintaining their keyed relationships.</p>



<p class="wp-block-paragraph">While EF Core doesn’t offer a `Copy` method, there are simple ways to duplicate an entry in a database. However, database schemas often use multiple entities, linked by foreign keys, to represent a single conceptual item. Duplicating an item of this kind is more complicated since a user must copy all the sub-entities and then update their foreign keys.</p>



<p class="wp-block-paragraph">Fortunately, EF Core can be manipulated to make this duplication trivial. Using the concept of <strong><em>detached entities</em>,</strong> EF Core can duplicate entities and automatically update their foreign keys.</p>



<h2 class="wp-block-heading" id="h-detached-entities">Detached Entities</h2>



<p class="wp-block-paragraph"><strong>Detached Entities</strong> are entities that no longer correspond to data in the database. When EF Core returns entities from a database query, these entities are, by default, tracked relative to the database. If a property on a tracked entity is changed and saved, the corresponding data in the database will be changed. However, we can tell EF Core to <strong><em>detach</em></strong> entities after they have been fetched.</p>



<p class="wp-block-paragraph">Once detached, an entity no longer corresponds to an entry in the database. Changes to the object will not be applied to the database when <strong><em>SaveChanges</em></strong> is called. EF Core will view these objects as entirely unrelated to the database.</p>



<h2 class="wp-block-heading" id="h-detach-and-duplicate">Detach and Duplicate</h2>



<p class="wp-block-paragraph">Once detached, you can add entities back to the database. Since EF Core is no longer tracking them, they will be viewed as new pieces of data. Consequently, a detached entity that is added and saved will result in a new entry in the database. Using this idea, we can copy data by fetching an entity, detaching the entity, and then adding it back to the database (and saving).</p>



<p class="wp-block-paragraph"><u><strong>Note:</strong></u> If you are using auto-incrementing primary keys, the primary key of the duplicate entities will need to be set to <strong><em>0</em> </strong>before adding them back to the database. This avoids adding duplicate primary keys to the database (which will cause the database to throw an exception when saving). <strong>See Step 2</strong> below for a generic function to handle detaching and resetting primary keys.</p>



<h2 class="wp-block-heading" id="h-handling-foreign-keys">Handling Foreign Keys</h2>



<p class="wp-block-paragraph">Duplicating data with this technique will automatically update the new data’s foreign keys. If a duplicate entity’s foreign key points at data that is not being duplicated, then that foreign key will stay the same.</p>



<p class="wp-block-paragraph">However, if a foreign key is pointed at another piece of data that <strong><em>has</em> </strong>been detached, the foreign key will be updated to point at the new, duplicate data. This way, if the database duplicates related entities in the same transaction, their relationships will be duplicated as well.</p>



<h2 class="wp-block-heading" id="h-example">Example</h2>



<p class="wp-block-paragraph">Here is the idea in practice. In this example, a <em>League</em> is made up of <em>Teams</em> that are made up of <em>Players.</em></p>



<p class="wp-block-paragraph"><strong>Step One: Fetch the Entities</strong></p>



<p class="wp-block-paragraph">First, we select the entities we would like to duplicate from the database</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C#</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>	 var league = db.Leagues.FirstOrDefault(l => l.Name == “EPL”);
     var teams = league.Teams.ToList();
     var players = teams.SelectMany(t => t.Players).ToList();</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">	 </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">league</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Leagues</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">FirstOrDefault</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">l</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #9CDCFE">l</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Name</span><span style="color: #D4D4D4"> == “</span><span style="color: #9CDCFE">EPL</span><span style="color: #D4D4D4">”);</span></span>
<span class="line"><span style="color: #D4D4D4">     </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">teams</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">league</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Teams</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">ToList</span><span style="color: #D4D4D4">();</span></span>
<span class="line"><span style="color: #D4D4D4">     </span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">players</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">teams</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">SelectMany</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">t</span><span style="color: #D4D4D4"> =&gt; </span><span style="color: #9CDCFE">t</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Players</span><span style="color: #D4D4D4">).</span><span style="color: #DCDCAA">ToList</span><span style="color: #D4D4D4">();</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>Step Two: Detach the Entities</strong></p>



<p class="wp-block-paragraph">Using the helper functions <strong><em>DetachEntity</em> </strong>and <strong><em>DetachEntities</em>,</strong> we detach our fetched entities. The database duplicates any entity that is detached. If an entity is detached, and it is related to another detached entity, that relationship will be duplicated between the two new entities.</p>



<p class="wp-block-paragraph">Note: The detach functions also set the primary key (“Id” in this case) to 0 since we are using auto-incrementing primary keys.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C#</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>private T DetachEntity&lt;T>(T entity, ConfigDbContext db) where T : class
{
    db.Entry(entity).State = EntityState.Detached;
    if (entity.GetType().GetProperty("Id") != null)
    {
        entity.GetType().GetProperty("Id").SetValue(entity, 0);
    }
    return entity;
}

private List&lt;T> DetachEntities&lt;T>(List&lt;T> entities, ConfigDbContext db) where T : class
{
    foreach (var entity in entities)
    {
        this.DetachEntity(entity, db);
    }
    return entities;
}
this.DetachEntity(league, db);
this.DetachEntities(teams, db);
this.DetachEntities(players, db);	</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">private</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">DetachEntity</span><span style="color: #D4D4D4">&lt;</span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4">&gt;(</span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">, </span><span style="color: #4EC9B0">ConfigDbContext</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4"> : </span><span style="color: #569CD6">class</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">Entry</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">).</span><span style="color: #9CDCFE">State</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">EntityState</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Detached</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">GetType</span><span style="color: #D4D4D4">().</span><span style="color: #DCDCAA">GetProperty</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Id&quot;</span><span style="color: #D4D4D4">) != </span><span style="color: #569CD6">null</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">GetType</span><span style="color: #D4D4D4">().</span><span style="color: #DCDCAA">GetProperty</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&quot;Id&quot;</span><span style="color: #D4D4D4">).</span><span style="color: #DCDCAA">SetValue</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">private</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">List</span><span style="color: #D4D4D4">&lt;</span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4">&gt; </span><span style="color: #DCDCAA">DetachEntities</span><span style="color: #D4D4D4">&lt;</span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4">&gt;(</span><span style="color: #4EC9B0">List</span><span style="color: #D4D4D4">&lt;</span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4">&gt; </span><span style="color: #9CDCFE">entities</span><span style="color: #D4D4D4">, </span><span style="color: #4EC9B0">ConfigDbContext</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">T</span><span style="color: #D4D4D4"> : </span><span style="color: #569CD6">class</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">foreach</span><span style="color: #D4D4D4"> (</span><span style="color: #569CD6">var</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">in</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">entities</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">this</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">DetachEntity</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">entity</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">entities</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"><span style="color: #569CD6">this</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">DetachEntity</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">league</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #569CD6">this</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">DetachEntities</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">teams</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #569CD6">this</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">DetachEntities</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">players</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">);	</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>Step Three: Add the Detached Entities Back to the Database</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C#</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>  await db.Leagues.AddAsync(league);
  await db.Teams.AddRangeAsync(teams);
  await db.TestPropertyGroups.AddRangeAsync(players);</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">await</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Leagues</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">AddAsync</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">league</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">await</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">Teams</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">AddRangeAsync</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">teams</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">await</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">TestPropertyGroups</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">AddRangeAsync</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">players</span><span style="color: #D4D4D4">);</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>Step Four: Save Changes</strong></p>



<p class="wp-block-paragraph">We have duplicated the league “EPL” and its teams and players. The relationships between the teams, players, and league have also been duplicated. So, each duplicate player is tied to a duplicate team, and each duplicate team is tied to the new, duplicate league.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C#</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>	await db.SaveChangesAsync();</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">	</span><span style="color: #569CD6">await</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">db</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">SaveChangesAsync</span><span style="color: #D4D4D4">();</span></span></code></pre></div>



<p class="wp-block-paragraph">This technique makes Entity Framework do most of the work and saves us from explicitly updating several primary keys. Any related entities that are detached together will have their relationship duplicated as well.</p>



<p class="wp-block-paragraph"><strong>Learn more about <a href="/services/application-development">DMC&#8217;s Application Development Services</a>&nbsp;as well as our <a href="/services/application-development/pc-application-development/c-net">C#</a> and&nbsp;<a href="/services/application-development/web-application-development/aspnet-and-aspnet-mvc">ASP.NET</a> expertise. <a href="/contact">Contact us</a> with any questions or inquiries.&nbsp;</strong></p>
<p>The post <a href="https://static.dmcinfo.com/blog/21028/duplicating-related-entities-using-entity-framework-core/">Duplicating Related Entities using Entity Framework Core</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
