[英]Why am I getting this error in ASP.NET Core 6 C#?
When I want to create a controller I get this error:当我想创建一个控制器时,我得到了这个错误:
enter image description here在此处输入图像描述
There was an error running the selected code generator:
运行所选代码生成器时出错:
'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Db.ContextOptions'1[DATAMain.DB] while attempting to activate 'DataMain.DB''尝试激活'DataMain.DB'时无法解析'Microsoft.EntityFrameworkCore.Db.ContextOptions'1 [DATAMain.DB]类型的服务
DB.cs数据库文件
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataMain
{
public class DB : IdentityDbContext<IdentityUser, IdentityRole, string>
{
public DB(DbContextOptions<DB> options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
}
}
Be sure to have the Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore package installed.确保安装了 Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 包。
then register the service :然后注册服务:
using ContosoUniversity.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ContosoUniversity
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
--> services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
--> services.AddDatabaseDeveloperPageExceptionFilter();
services.AddControllersWithViews();
}
read documentation on microsoft : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-6.0阅读有关微软的文档: https ://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-6.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.