MySql.EntityFramework 找不到合适的构造函数

     2023-03-29     228

关键词:

【中文标题】MySql.EntityFramework 找不到合适的构造函数【英文标题】:MySql.EntityFramework a suitable constructor could not be located 【发布时间】:2022-01-11 14:51:14 【问题描述】:

这是我第一次尝试使用 EntityFramework 对 DB 进行操作。

我的数据库是在 MySQL 中设置的,所以我已经为它安装了所有需要的 NuGets 包,我试图设置 MySql.EntityFrameworkCore,但是当我尝试运行该程序时,我收到以下错误:

System.AggregateException: '有些服务不能 构造(验证服务描述符时出错 '服务类型:VisualPos.Data.MyDbContext 寿命:范围 ImplementationType:VisualPos.Data.MyDbContext':一个合适的 'VisualPos.Data.MyDbContext' 类型的构造函数不能是 位于。确保类型是具体的并且已注册服务 公共构造函数的所有参数。)'

我的代码如下所示:

程序.cs

using Microsoft.EntityFrameworkCore;
using VisualPos.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<MyDbContext>(options => options.UseMySQL(builder.Configuration.GetConnectionString("Default")));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build(); // CODE CRASH HERE

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())

    app.UseSwagger();
    app.UseSwaggerUI();


app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

MyDbContext.cs

使用 Microsoft.EntityFrameworkCore; 使用 VisualPos.Models;

namespace VisualPos.Data

    public class MyDbContext : DbContext
    
        protected MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        
        
        
        public DbSet<Cfg> Cfg  get; set; 

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        
            base.OnModelCreating(modelBuilder);

        
    

然后在我的控制器中,我将执行 _context.Cfg.ToList(); ,其中 _contextMyDbContext 上下文。

cfg 表还存在于 DB 中。

【问题讨论】:

您使用哪些 NuGet 包(用于 MySQL 和 EF Core)和版本? @BradleyGrainger MySql.Data 8.0.27、MySql.EntityFrameworkCore 6.0.0-preview3.1 和 Microsoft.EntityFrameworkCore 6.0.0 【参考方案1】:

解决方法在错误信息中:

“VisualPos.Data.MyDbContext”类型的合适构造函数不能 位于。确保类型是具体的并且服务已注册 公共构造函数的所有参数。

你的构造函数是protected。应该是:

public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)

【讨论】:

找不到名为“my”的模块。找不到名为“mycontroller”的控制器

】找不到名为“my”的模块。找不到名为“mycontroller”的控制器【英文标题】:Cannotfindmodulewithname\'my\'.Cannotfindcontrollerwithname\'mycontroller\'【发布时间】:2016-05-1601:33:03【问题描述】:您好,我收到以下错误:错误:找不到名为“m... 查看详情

颤振:“致命错误:找不到模块'位置'”Xcode找不到模块

】颤振:“致命错误:找不到模块\\\'位置\\\'”Xcode找不到模块【英文标题】:flutter:"fatalerror:module\'location\'notfound"Xcodecannotfoundmodules颤振:“致命错误:找不到模块\'位置\'”Xcode找不到模块【发布时间】:2022-01-1607:36:28【... 查看详情

找不到模块“快递”;找不到名称“进程”;等等

】找不到模块“快递”;找不到名称“进程”;等等【英文标题】:Cannotfindmodule\'express\';Cannotfindname\'process\';andsoon【发布时间】:2016-03-1314:49:31【问题描述】:我是VisualStudioCode的新手,从VisualStudio2015开始横向迁移。我正在使用... 查看详情

Pip:找不到版本。找不到匹配的分布

】Pip:找不到版本。找不到匹配的分布【英文标题】:Pip:couldnotfindaversion.Nomatchingdistributionfound【发布时间】:2017-08-1616:26:05【问题描述】:我正在尝试安装Flask-ACL:https://mikeboers.github.io/Flask-ACL$pipsearchacl|grep-iflaskFlask-ACL(0.0.1)-Acce... 查看详情

找不到引用的类/找不到引用的方法

】找不到引用的类/找不到引用的方法【英文标题】:Can\'tfindreferencedclass/Can\'tfindreferencedmethod【发布时间】:2014-09-2809:15:22【问题描述】:我以为我有点理解proguardkeep规则,但我想我不明白!我的proguard配置文件中有一些如下形... 查看详情

Angular 和 Typescript:找不到名称 - 错误:找不到名称

】Angular和Typescript:找不到名称-错误:找不到名称【英文标题】:AngularandTypescript:Can\'tfindnames-Error:cannotfindname【发布时间】:2016-01-2418:20:17【问题描述】:我将Angular(版本2)与TypeScript(版本1.6)一起使用,当我编译代码时出现... 查看详情

找不到localhostweb页面

参考技术A找不到localhostweb页面?答:找不到,不用着急。慢慢来,根据提示分体操作。 查看详情

Laravel 找不到样式表,我也找不到

】Laravel找不到样式表,我也找不到【英文标题】:LaravelcannotfindstylesheetsandnorcanI【发布时间】:2020-08-0601:15:05【问题描述】:所以我对Laravel很陌生,我正在学习使用身份验证功能的教程。这工作正常,但它无法找到我的脚本和... 查看详情

ldd查看符号找不到

ldd查看符号找不到 查看程序程序中那些符号找不到 查看详情

既找不到 .cubin 也找不到 .ptx 文件编译 CUDA

】既找不到.cubin也找不到.ptx文件编译CUDA【英文标题】:Can\'tfindneither.cubinnor.ptxfilecompilingCUDA【发布时间】:2015-10-1723:27:31【问题描述】:我正在使用VisualStudio2013开发CUDA(7.0)项目。该项目是64位的。我正在使用驱动程序API,需要... 查看详情

找不到 git 命令,也找不到 ssh-agent

】找不到git命令,也找不到ssh-agent【英文标题】:gitcommandcouldnotbefoundandcouldnotfindssh-agent【发布时间】:2013-02-0317:06:58【问题描述】:我今天在GitHub上,并意识到我落后于来自repo的更新。我尝试获取更新,但从一开始就失败了。... 查看详情

为啥找不到 getservletcontext?

】为啥找不到getservletcontext?【英文标题】:Whygetservletcontextisn\'tfound?为什么找不到getservletcontext?【发布时间】:2011-06-1722:55:10【问题描述】:我正在尝试使用getServletContext().getRealPath("/"),但我不断收到此错误:找不到符号符号... 查看详情

/cs/software/anaconda3/compiler_compat/ld: 找不到 -lm 找不到 -lpthread 找不到 -lc

】/cs/software/anaconda3/compiler_compat/ld:找不到-lm找不到-lpthread找不到-lc【英文标题】:/cs/software/anaconda3/compiler_compat/ld:cannotfind-lmcannotfind-lpthreadcannotfind-lc【发布时间】:2017-10-2717:01:20【问题描述】:我应该如何在CentOS7中修复这个错... 查看详情

打字稿编译器找不到带有纱线的节点-找不到“节点”的类型定义文件

】打字稿编译器找不到带有纱线的节点-找不到“节点”的类型定义文件【英文标题】:Typescriptcompilercan\'tfindnodewithyarn-Cannotfindtypedefinitionfilefor\'node\'【发布时间】:2021-10-2705:56:32【问题描述】:我已经安装了@types/node。我正在使... 查看详情

/usr/bin/ld: 找不到 -ldlib /usr/bin/ld: 找不到 -lcblas /usr/bin/ld: 找不到 -llapack

】/usr/bin/ld:找不到-ldlib/usr/bin/ld:找不到-lcblas/usr/bin/ld:找不到-llapack【英文标题】:/usr/bin/ld:cannotfind-ldlib/usr/bin/ld:cannotfind-lcblas/usr/bin/ld:cannotfind-llapack【发布时间】:2021-05-0710:08:46【问题描述】:/usr/bin/ld:cannotfind-ldli 查看详情

错误:找不到模块“app/MyComponent/MyComponent.module”错误:找不到模块

】错误:找不到模块“app/MyComponent/MyComponent.module”错误:找不到模块【英文标题】:Error:Cannotfindmodule\'app/MyComponent/MyComponent.module\'Error:Cannotfindmodule【发布时间】:2018-12-3007:52:13【问题描述】:这与提到的here非常相似,但是cmets... 查看详情

intellijidea编译代码报错找不到符号符号:找不到符号包包

在使用IDEA的时候,经常出现过找不到包或者找不到符号的情况,可以尝试以下几种方式来解决1.如果项目使用的是Maven可以使用Maven-Reimport2.还可以InvalidateandRestart(无效并重新启动) 3.统一编码格式 4.重新编译  1.打开P... 查看详情

springboot找不到类异常解决

SpringBoot找不到类异常解决 问题描述:项目是一个Maven+SpringBoot项目,在Eclipse中运行正常,没有问题,但是用IDEA编辑的时候根本启动不了,报错一大堆,比如找不到HttpServletRequest类,找不到Hibernate的类,和找不到Tomcat初始化类... 查看详情