How to fix DbContextOptions resolving properly from services to apply EF migration to create database?

Vatai Domonkos 10 Reputation points
2024-04-05T22:58:14.0533333+00:00

I'm trying to run a migration to create my initial database from code, but i'm having difficulties with proper registration of DbContextOptions.

My program.cs contains the service registration:

var builder = WebApplication.CreateBuilder(args);  
// Add services to the container. 
// Registering the DbContext
var connectionString = builder.Configuration.GetConnectionString("EduSqlServer");
builder.Services.AddDbContext<ApplicationDbContext>(
    options => options.UseSqlServer(connectionString, b => b.MigrationsAssembly("data_access_layer"))); 

Note: program.cs is in the education.Server assembly.

Appsettings.json:

  "ConnectionStrings": {
    "EduSqlServer": "Server=.;Database=EduDb;Trusted_Connection=True;Encrypt=False"
  },

I have a different assembly called "data_access_layer" where i have my DbContext.
Here's ApplicationDbContext.cs:

    public class ApplicationDbContext : IdentityDbContext<IdentityUser>
    {
 
	public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :
            base(options)
        {

        }

When running the 'Add-Migration CreateDb' command in my package manager console I'm getting an error:

Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[data_access_layer.Domain.Context.ApplicationDbContext]' while attempting to activate 'data_access_layer.Domain.Context.ApplicationDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I noticed that if i create an empty constructor for my ApplicationDbContext and in the OnConfiguring method of ApplicationDbContext i place the exact same code that i did have in my program.cs than i can create a migration.

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        {             				optionsBuilder.UseSqlServer("Server=.;Database=EduDb;Trusted_Connection=True;Encrypt=False", b => b.MigrationsAssembly("data_access_layer"));
         }

This works for now, but i obviously don't want to hardcode my sql server's connection string for my final application. I'd like for it to work through proper service resolve. How could i fix this issue?

I was trying to follow: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-8.0
but wanted to use sql server instead of in-memory database.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Krystian Wawruch 0 Reputation points
    2024-04-14T19:55:59.5633333+00:00

    Hi,
    I had same problem these days and this solution works for me so far

    1. Add EF Core Tools or Desing to your Main Project (for example by Nuget Package Manager)
    2. Set your main project as a start-up project
    3. If you are using EF Tools - in console packets manager set your data project as default project
    0 comments No comments