我有一个 Asp.Net 项目,我尝试使用 DbContext 迁移数据库。不幸的是,我收到错误
无法创建类型为“”的“DbContext”。尝试激活“Bulky.DataAccess.Data.ApplicationDbContext”时出现异常“无法解析类型“Microsoft.EntityFrameworkCore.DbContextOptions`1[Bulky.DataAccess.Data.ApplicationDbContext]”的服务。尝试创建实例时抛出。
当我尝试使用以下命令迁移数据库时
dotnet ef 迁移添加 AddCategoryToDbAndSeedTable
命令
你能向我解释一下为什么它不起作用吗?我用谷歌驱动器链接附加了整个项目
项目链接:https://drive.google.com/file/d/1hEIv9BeocKmFZRyELYeUZTTtyrIaZbF9/view ?usp=sharing
编辑:这是详细日志: https://drive.google.com/file/d/1jPweHJc0YsI-Kg_5W6D5cfFQd3n6X8XU/view ?usp=sharing
这是相关代码:
程序.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 }
);
}