I have a Asp.Net project where I try to migrate a Database with a DbContext.
Unfortunately, I get the Error
Unable to create a ‘DbContext’ of type ”. The exception ‘Unable to resolve service for type ‘Microsoft.EntityFrameworkCore.DbContextOptions`1[Bulky.DataAccess.Data.ApplicationDbContext]’ while attempting to activate ‘Bulky.DataAccess.Data.ApplicationDbContext’.’ was thrown while attempting to create an instance.
when I try to migrate the database with the
dotnet ef migrations add AddCategoryToDbAndSeedTable
command
Can you explain to me why it’s not working?
I attached the whole project with a google drive link
Project link: https://drive.google.com/file/d/1hEIv9BeocKmFZRyELYeUZTTtyrIaZbF9/view?usp=sharing
Edit:
And here are the verbose logs:
https://drive.google.com/file/d/1jPweHJc0YsI-Kg_5W6D5cfFQd3n6X8XU/view?usp=sharing
Here is the relevant code:
Program.cs:
using Bulky.DataAccess.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
ApplicationDbContext.cs:
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>().HasData(
new Category { Id = 1, Name = "Action", DisplayOrder = 1 },
new Category { Id = 2, Name = "SciFi", DisplayOrder = 2 },
new Category { Id = 3, Name = "History", DisplayOrder = 3 }
);
}
2
Answers
You need to specify a
--project
(the project to which the new EF migration should be added) and--startup-project
(the project which contains the DB settings) to run the migration against. Without these settings, theBulky.DataAccess
project does not have the DB connection string and theApplicationDbContext
cannot be constructed without dependency injection setup from theBulkyWeb3
project.From the same folder as BuklyWeb3.sln, you can run this command:
dotnet ef migrations add AddInitialMigration --startup-project BulkyWeb3 --project Bulky.DataAccess
Or, from the same folder as Bulky.DataAccess.csproj, you can run this command:
dotnet ef migrations add AddInitialMigration --startup-project ..BulkyWeb3
These commands will create a new EF migration in the new
/BulkyWeb3/Bulky.DataAccess/Migrations
folder.What you need is an
IDesignTimeDbContextFactory<ApplicationDbContext>
which will provice a connection string to yourApplicationDbContext
while working with thedotnet ef
cli tools.The connection string can just point to localdb, since
dotnet ef migrations add
does not connect to the database, it just needs to be able to instantiate yourDbContext
.