简体   繁体   English

为什么我在 ASP.NET Core 6 C# 中收到此错误?

[英]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.

相关问题 ASP.NET Core Web API &amp; Entity Framework Core; 为什么我收到这个错误? - ASP.NET Core Web API & Entity Framework Core; why am I getting this error? 为什么我在使用C#的asp.net中的密码字段中获取旧密码? - why I am getting old password in a password field in asp.net using C#? 为什么我没有使用c#在我的asp.net应用程序中获取.csv文件? - why i am not getting the .csv file in my asp.net application using c#? 为什么在ASP.NET中的Web配置中出现错误? - Why am I getting an error in web config in ASP.NET? 为什么在 C# .Net Core 3 中连接到 MySql 时出现错误 - Why am I getting an error when connecting to MySql in C# .Net Core 3 为什么我在 Asp.Net Core 日志中收到“不支持 POST 请求”? - Why am I getting “POST requests are not supported” in my Asp.Net Core log? 我正在尝试在 asp.net c# 中使用 google API 发送 email 并收到错误 400 redirect_uri_mismatch - I am trying to send email using google API in asp.net c# and getting Error 400 redirect_uri_mismatch 我在基于 Web 的项目上使用 C# 处理 ASP.NET,但出现编译错误 - I am working on the ASP.NET with C# on the Web based project and I am having the compliation error C# ASP.NET Core:我只能更新一个表的数据,但其他表没有更新 - C# ASP.NET Core : I am able to update data for only one table but the others are not updating 收到错误消息:CS0029:无法隐式转换(ASP.NET Core MVC 和 C#) - Getting error message: CS0029: Cannot implicitly convert (ASP.NET Core MVC & C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM