<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikola Jovanovic on Medium]]></title>
        <description><![CDATA[Stories by Nikola Jovanovic on Medium]]></description>
        <link>https://medium.com/@nikjov92?source=rss-f3b16a9b3e76------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W_KJEZklaCJVqmJ23DMDqA.jpeg</url>
            <title>Stories by Nikola Jovanovic on Medium</title>
            <link>https://medium.com/@nikjov92?source=rss-f3b16a9b3e76------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:33:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikjov92/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Effectively separate EF Core migrations into a separate project (.NET 8)]]></title>
            <link>https://medium.com/@nikjov92/effectively-separate-ef-core-migrations-into-a-separate-project-net-8-2d84843d17da?source=rss-f3b16a9b3e76------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d84843d17da</guid>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[database-migration]]></category>
            <dc:creator><![CDATA[Nikola Jovanovic]]></dc:creator>
            <pubDate>Thu, 23 Jan 2025 19:25:10 GMT</pubDate>
            <atom:updated>2025-02-08T11:03:21.379Z</atom:updated>
            <content:encoded><![CDATA[<h3>Separating Database Migrations in .NET: Enhancing Project Organization and Maintainability</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZfsOJUUZ5adxzGCA" /><figcaption>Photo by <a href="https://unsplash.com/@hgudka97?utm_source=medium&amp;utm_medium=referral">Harshil Gudka</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The Case for Independent Migration Projects</h3><p>Managing database schema changes can become a significant challenge when developing complex software systems. While it’s possible to incorporate migrations directly into the main application project, separating them into dedicated projects offers several advantages.</p><p>Key benefits include:</p><ul><li><strong>Clear Separation of Concerns: </strong>Isolating migrations prevents clutter in the main application and promotes a more organized codebase.</li><li><strong>Environment-specific Migrations: </strong>Different environments (e.g., development, testing, production) may require distinct migration sets to accommodate varying data requirements.</li><li><strong>Provider flexibility: </strong>Supporting multiple database providers (e.g., SQL Server, PostgreSQL, SQLite) becomes more manageable with independent migration projects.</li></ul><p>This article will guide you through creating a separate migration project, adding an initial migration, and executing it using Docker. We’ll use Microsoft SQL Server and the Blog example-inspired project from Microsoft as our demonstration which you can find on <a href="https://github.com/njovanovic-se/Separate-Migrations-Article">my GitHub account</a>.</p><h3>Prerequisites</h3><h4>Software:</h4><ul><li><strong>Docker for Windows:</strong> A platform for building, shipping, and running applications.</li><li><strong>Visual Studio Community Edition:</strong> A free integrated development environment for .NET development.</li><li><strong>Microsoft SQL Server Docker image: </strong>A pre-configured image containing Microsoft SQL Server.</li></ul><h4>Knowledge:</h4><ul><li><strong>C#:</strong> A general-purpose programming language used for .NET development.</li><li><strong>.NET:</strong> A cross-platform framework for building modern web applications.</li><li><strong>Entity Framework Core:</strong> An object-relational mapper (ORM) for .NET that simplifies database interactions.</li><li><strong>Docker Basics:</strong> A fundamental understanding of Docker concepts, including images, containers, and Docker Compose.</li></ul><p><strong>Note:</strong> While not strictly required you can apply these steps to your project and have the same result.</p><h3>Preparing the environment</h3><h4>Running the SQL Server Docker image:</h4><ul><li>Open the terminal or command prompt.</li><li>Pull the Microsoft SQL Server Docker image:</li></ul><pre>docker pull mcr.microsoft.com/mssql/server:2022-latest</pre><ul><li>Create and run a Docker container for SQL Server:</li></ul><pre>docker run -e &quot;ACCEPT_EULA=Y&quot; -e &quot;MSSQL_SA_PASSWORD=StrongestPass@word&quot; -e &quot;MSSQL_PID=Developer&quot; -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest</pre><p><strong>Note: </strong>Replace `StrongestPass@word` with a secure password.</p><p>Next steps: Connect to the SQL Server, create a separate project for migrations, and run the initial migration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*uUGBjubdfz6v6RBuDwrGIw.png" /><figcaption>Solution structure before creating the Migrations project</figcaption></figure><h4>Open the project:</h4><ul><li>Launch Visual Studio Community Edition.</li><li>Open the Blog example project</li></ul><h4>Create a Migration Project (if necessary):</h4><ul><li>Right-click on the solution in Visual Studio.</li><li>Select “<em>Add</em>” &gt; “<em>New Project</em>”.</li><li>Choose “<em>Class Library</em>” and give it a name like “<em>&lt;SolutionName&gt;.Migrations</em>”.</li><li>Install the <em>Entity Framework Core</em> package within the Infrastructure project (or within the project where your DbContext is):</li></ul><pre>dotnet add package Microsoft.EntityFrameworkCore</pre><ul><li>Install the database provider package within the ‘<em>API</em>’ and ‘<em>Migrations</em>’ projects:</li></ul><pre>dotnet add package Microsoft.EntityFrameworkCore.SqlServer</pre><ul><li>Install the package necessary for migrations within the ‘<em>Migrations</em>’ project:</li></ul><pre>dotnet add package Microsoft.EntityFrameworkCore.Design</pre><ul><li>Install the package within the ‘<em>API’</em> project that enables you to run <strong>Add-Migration</strong> command from API project:</li></ul><pre>dotnet add package Microsoft.EntityFrameworkCore.Tools</pre><p><strong>Note: </strong>Assuming the necessary packages have been installed correctly, you should be able to execute migrations directly from the API project. However, if you encounter any syntax errors (indicated by squiggly lines within the code editor), please rebuild the project to resolve the issue.</p><h3>Connecting Projects in the Solution</h3><h4>Add a Reference from the Migration Project to the Infrastructure Project:</h4><ul><li>Right-click on the “Medium.Demonstration.Migrations” project in Solution Explorer.</li><li>Select “Add” &gt; “Reference”.</li><li>Choose the “Medium.Demonstration.Infrastructure” project and click “OK”.</li></ul><h4>Add a Reference from the API Project to the Migration Project:</h4><ul><li>Right-click on the “Medium.Demonstration.API” project in Solution Explorer.</li><li>Select “Add” &gt; “Reference”.</li><li>Choose the “Medium.Demonstration.Migrations” project and click “OK”.</li></ul><p>By establishing these references, you’ll ensure that the API project can access the migration scripts defined in the Migration project and that the Migration project can leverage the infrastructure components in the Infrastructure project.</p><h4>Adding necessary code to Program.cs</h4><pre>using Medium.Demonstration.Infrastructure;<br>using Microsoft.EntityFrameworkCore;<br><br>var builder = WebApplication.CreateBuilder(args);<br><br>// Add services to the container.<br><br>builder.Services.AddControllers();<br>// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle<br>builder.Services.AddEndpointsApiExplorer();<br>builder.Services.AddSwaggerGen();<br><br>builder.Services.AddDbContext&lt;MediumDemoContext&gt;(options =&gt;<br>{<br>    options.UseSqlServer(builder.Configuration.GetConnectionString(&quot;MediumDemoSql&quot;), opts =&gt;<br>    {<br>        opts.MigrationsAssembly(&quot;Medium.Demonstration.Migrations&quot;);<br>    });<br>});<br><br>var app = builder.Build();<br><br>// Configure the HTTP request pipeline.<br>if (app.Environment.IsDevelopment())<br>{<br>    app.UseSwagger();<br>    app.UseSwaggerUI();<br>}<br><br>app.UseHttpsRedirection();<br><br>app.UseAuthorization();<br><br>app.MapControllers();<br><br>app.Run();</pre><p>Explanation:</p><ol><li><strong>Add DbContext: </strong>The AddDbContext method registers the ApplicationDbContext with the dependency injection container.</li><li><strong>Configure Migration Assembly:</strong> The MigrationsAssembly method configures the assembly where the migrations are maintained for this context.</li></ol><p><strong>Note: </strong>Ensure your connection string is configured to point to the Docker SQL container we previously ran.</p><h4>Adding the migration</h4><p>To incorporate new migration into the existing set, utilize the following command within the Package Manager Console in Visual Studio:</p><pre>Add-Migration MIGRATION_NAME -Project Medium.Demonstration.Migrations</pre><p>For developers more accustomed to the .NET CLI, the Add-Migrationcommand can be alternatively executed using the following syntax:</p><pre>dotnet ef migrations add MIGRATION_NAME --project Medium.Demonstration.Migrations --startup-project Medium.Demonstration.API</pre><p>This command achieves the same result of adding a new migration but leverages the .NET CLI for those who prefer this approach.</p><h4>Considerations</h4><p>Some considerations when implementing a separate migration project include:</p><ul><li><strong>CI/CD pipelines</strong> — careful attention must be paid to the order of build and execution of tasks within your CI/CD pipelines</li><li><strong>Docker Image Optimization</strong> — If employing Docker for deployment, optimizing Docker images is crucial for efficient production environments and streamlined maintenance</li><li><strong>Organizational Constraints</strong> — Some organizations may choose to go with a dedicated console application for managing migrations due to infrastructural limitations, such as restricted privileges within certain environments</li></ul><p>By thoroughly evaluating infrastructural constraints and business requirements, coupled with applying industry best practices, you can make an informed decision regarding the optimal approach to handling and developing migrations within your project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d84843d17da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>