Resolving EF Migration Errors with a Separate Database Project

As developers, we’ve all been there – stuck on a seemingly insurmountable error, with no clear solution in sight. Recently, I encountered an issue while trying to run migrations on my ASP.NET Core application. The problem was that my database was housed in a separate project from my application, which led to some unexpected errors.

After some digging, I realized that the solution lay in creating a factory to handle the configuration of my DbContext. In this article, I’ll walk you through the steps I took to resolve the issue and provide a working solution.

The Problem

When I attempted to run migrations, I received a series of cryptic errors that didn’t provide much insight into the root cause of the problem. The issue stemmed from the fact that my database project was separate from my application project. This meant that the migration configuration was not being picked up correctly.

The Solution

To resolve the issue, I created a factory to handle the configuration of my DbContext. This involved overriding the OnConfiguring method in my DbContext class. Here’s the code:

csharpCopyInsertprotected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        // Build configuration
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

        var connectionString = configuration.GetConnectionString("DefaultConnection");
        optionsBuilder.UseSqlServer(connectionString, sqlOption =>
        {
            sqlOption.MigrationsAssembly("Aspnetcoreapp");
            sqlOption.UseNetTopologySuite();
        });
    }
}

Let’s break down what’s happening in this code:

  • We first check if the optionsBuilder is already configured. If not, we proceed to build the configuration.
  • We create an instance of IConfigurationRoot using the ConfigurationBuilder class. We set the base path to the current directory and add the appsettings.json file as a configuration source.
  • We retrieve the connection string from the configuration using the GetConnectionString method.
  • We use the UseSqlServer method to configure the DbContext to use SQL Server. We pass in the connection string and a lambda expression that configures the SQL options.
  • In the lambda expression, we specify the migrations assembly using the MigrationsAssembly method. This is important, as it tells Entity Framework where to find the migrations.
  • Finally, we use the UseNetTopologySuite method to enable support for spatial data types.

Conclusion

By creating a factory to handle the configuration of my DbContext, I was able to resolve the migrations errors and get my application up and running. This solution should be helpful to anyone who is experiencing similar issues with separate database and application projects. Remember to update the MigrationsAssembly method to match the name of your migrations assembly.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *