<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Quailoop Software]]></title><description><![CDATA[Self-employed, passionate developer with strong backend skills.]]></description><link>https://blog.quailoop.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1666044383309/pb3icKkfq.png</url><title>Quailoop Software</title><link>https://blog.quailoop.com</link></image><generator>RSS for Node</generator><lastBuildDate>Mon, 18 May 2026 19:51:42 GMT</lastBuildDate><atom:link href="https://blog.quailoop.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Merging Incremental Migrations in Entity Framework]]></title><description><![CDATA[The story
Have you ever encountered a challenge that looked something like this? You’re assigned to a task force with the mission of implementing a new, exciting feature in your legacy application. You’re grateful to your fellow developers for transi...]]></description><link>https://blog.quailoop.com/merging-incremental-migrations-in-entity-framework</link><guid isPermaLink="true">https://blog.quailoop.com/merging-incremental-migrations-in-entity-framework</guid><category><![CDATA[entity framework]]></category><category><![CDATA[.NET]]></category><category><![CDATA[Databases]]></category><category><![CDATA[code first]]></category><category><![CDATA[migrations]]></category><dc:creator><![CDATA[Konrad Przepiórzyński]]></dc:creator><pubDate>Tue, 26 Mar 2024 10:16:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/fPkvU7RDmCo/upload/c73d61ce29a72e3590f83247b56c1d9c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-story">The story</h2>
<p>Have you ever encountered a challenge that looked something like this? You’re assigned to a task force with the mission of implementing a new, exciting feature in your legacy application. You’re grateful to your fellow developers for transitioning from scripted migrations to the Entity Framework and Code-First approach.</p>
<p>You start to roll out new, incremental features. Naturally, these features bring about minor, progressive changes to your models. Along the way, you encounter bugs, and then more bugs… You diligently rectify them and adjust your model. Consequently, numerous incremental changes are made to your database - after all, testing is a must, right?</p>
<p>Then, after some time, your Migrations folder looks like this...</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711448674774/eab031c3-3a8e-4c21-9806-aac1c966b166.png" alt class="image--center mx-auto" /></p>
<p>Now, let’s address the elephant in the room… What do you mean these migrations are fake? Well, you’ve caught me… I cleared my migrations folder some time ago… These are merely the result of some clever copy-pasting and obfuscation. But let’s get back on track… What if I told you that you could consolidate all of these changes into one neat, streamlined migration with just a few Package Manager Console (PMC) commands and a single SQL statement? Sounds appealing? Let’s dive in!</p>
<h2 id="heading-the-solution">The solution</h2>
<p>Here is a step by step solution for you:</p>
<ol>
<li><p>First, you need to identify the migrations you wish to consolidate. These are typically incremental, so simply select the oldest one from the set that falls within the date-time span of the new feature development.</p>
</li>
<li><p>Following the date-time pattern, you can prepare your list of migrations.</p>
</li>
<li><p>Next, identify any manual modifications you’ve made to the <code>Up()</code> and <code>Down()</code> methods in specific migrations. If there are none, proceed to the next step.</p>
</li>
<li><p>Take note of or remember the oldest migration and delete it along the rest from your project. If your IDE does not automatically update your <code>*.csproj</code> file, ensure to remove the files from the project manifest.</p>
</li>
<li><p>Now, navigate to the target database and locate the <code>__MigrationHistory</code> table. Remove the relevant migrations from the table by executing the following script:</p>
<pre><code class="lang-sql"> <span class="hljs-keyword">DELETE</span> TOP(%numberOfMigrationsToDelete%)
 <span class="hljs-keyword">FROM</span> [YourDatabase].[dbo].[__MigrationHistory]
 <span class="hljs-keyword">WHERE</span> MigrationId <span class="hljs-keyword">LIKE</span> <span class="hljs-string">'2024%'</span> <span class="hljs-comment">--you can use any condition for filtering</span>
</code></pre>
</li>
<li><p>Next, add a single, consolidated migration:<br /> <code>PM&gt; Add-Migration MyCompleteFeature</code></p>
</li>
<li><p>To update the database, you can either comment out the <code>Up()</code> method in the <code>MyCompleteFeature</code> migration or proceed with applying the explicit migration and targeting the one that is immediately before the consolidated one. Then, update the database in the usual manner:</p>
<p> <code>PM&gt; Update-Database -Target [migration_before_consolidation]</code> or / and then</p>
<p> <code>PM&gt; Update-Database</code></p>
</li>
</ol>
<p>Done! Quite easy, right? Remember to run all unit tests and do some smoke testing before committing the changes to the repo.</p>
<h2 id="heading-closing-statement">Closing statement</h2>
<p>As with any major change, it’s essential to back up your database and codebase before making any potentially irreversible modifications. If you decide to apply old migrations and then reapply the new one, your data will be lost. So proceed with caution.</p>
<p>If you found this post helpful, please hit the like button and share it with your colleagues. If you have any thoughts or comments, I’d be delighted to read them in the comment section below. Thank you for reading!</p>
]]></content:encoded></item></channel></rss>