【发布时间】:2021-12-25 18:04:11
【问题描述】:
在我的 ASP.NET Core 应用程序中,我收到以下错误:
InvalidOperationException:无法解析服务类型 尝试激活时出现“mvc_net6.Service.DB_Context” 'mvc_net6.Controllers.StudentController'。
StudentController StudentController 是通过 Scaffolds 创建到视图的,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;
using mvc_net6.Service;
namespace mvc_net6.Controllers
{
public class StudentController : Controller
{
private readonly DB_Context _context;
public StudentController(DB_Context context)
{
_context = context;
}
// GET: Student
public async Task<IActionResult> Index()
{
return View(await _context.Students.ToListAsync());
}
// GET: Student/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.FirstOrDefaultAsync(m => m.Id == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// GET: Student/Create
public IActionResult Create()
{
return View();
}
// POST: Student/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,CPF,Email")] Student student)
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}
// GET: Student/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students.FindAsync(id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Student/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,CPF,Email")] Student student)
{
if (id != student.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(student);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(student.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(student);
}
// GET: Student/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.FirstOrDefaultAsync(m => m.Id == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Student/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var student = await _context.Students.FindAsync(id);
_context.Students.Remove(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool StudentExists(int id)
{
return _context.Students.Any(e => e.Id == id);
}
}
}
我有一个文件 DB_Context.cs,其中包含一个类及其实现,如下所示:
using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;
namespace mvc_net6.Service
{
public class DB_Context : DbContext
{
public DB_Context(DbContextOptions<DB_Context> options) : base(options) {}
public DbSet<mvc_net6.Models.Student> Students {get; set;}
}
}
我有一个文件 DB_Factory.cs,其中包含一个类及其实现,如下所示:
namespace mvc_net6
{
public class BloggingContextFactory : IDesignTimeDbContextFactory<DB_Context>
{
public DB_Context CreateDbContext(string[] args)
{
// Replace with your server version and type.
// Use 'MariaDbServerVersion' for MariaDB.
// Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
var serverVersion = new MariaDbServerVersion(new Version(10, 5, 12));
var optionsBuilder = new DbContextOptionsBuilder<DB_Context>();
optionsBuilder.UseMySql("ConnectionString", serverVersion);
return new DB_Context(optionsBuilder.Options);
}
}
}
My Program.cs 包含从模板默认生成的代码。
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
IConfiguration configuration = app.Configuration;
// 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();
//builder.Services.AddDbContext<DB_Context>(options =>
// options.UseMySql(connection, serverVersion));
builder.Services.AddDbContext<DB_Context>(opt =>
{
opt.UseMySql(connection, serverVersion);
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
但是,在运行系统时,我现在收到以下错误:
watch : Started
Unhandled exception. System.InvalidOperationException: Cannot modify ServiceCollection after application is built.
at Microsoft.AspNetCore.WebApplicationServiceCollection.CheckServicesAccess()
at Microsoft.AspNetCore.WebApplicationServiceCollection.Add(ServiceDescriptor item)
at Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(IServiceCollection collection, ServiceDescriptor descriptor)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddCoreServices[TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime optionsLifetime)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContext](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
at Program.<Main>$(String[] args) in /home/r31sr4r/Projects/dotnet_core/mvc_net6/Program.cs:line 31
watch : Exited with error code 134
watch : Waiting for a file to change before restarting dotnet...
【问题讨论】:
标签: c# asp.net-mvc .net-core