所选代码生成器出现错误 运行:无法解析类型 'Microsoft.EntityFrameworkCore.DbContextOption 的服务
There was an error running the selected code generator: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOption
我正在使用 Visual Studio 2022 预览版和 .NET 6 SDK。
这里我创建了一个 2 层的 webAPI 项目。
api 项目 (Bgvsystem.webAPI)
class 图书馆 (BgvSystem.Persistance)
NuGet 包-
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version
6.0.0-rc.1.21452.10
Install-Package Microsoft.EntityFrameworkCore.Tools -Version
6.0.0-rc.1.21452.10
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
-Version 6.0.0-rc.1.21464.1
当我尝试使用脚手架添加控制器时,出现以下错误
There was an error running the selected code generator: unable to resolve service for type 'microsoft.entityframeworkcore.dbcontextoption.. While attempting to activate Dbcontext in .net 6 and visual studio 2022 preview
如何解决这个问题?请帮忙解决这个问题。
您需要将 DBContext 添加到 startup.cs 文件中的服务集合或作为构造函数 BGvSystemContext class.
传递
折腾了3天,终于找到错误并修复了。
实际上我必须将连接字符串放在 MyApp.Persistance.DbContextClass 中,如下所示
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=DESKTOP-SV8GPJ2\SQLEXPRESS;Initial Catalog=StarterAppDB;Persist Security Info=True;User ID=sa;Password=Admin@1234");
}
}
然后它运行良好。
如果是 .NET 6
你能这样试试吗
Program.cs
builder.Services.AddDbContext<YourDbContext>(options =>
options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Server=YourServer; Database=YourDb; Integrated Security=true; MultipleActiveResultSets=true; Trusted_Connection=True"
}
YourDbContext.cs
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=YourServer; Database=YourDb; Integrated Security=true; MultipleActiveResultSets=true; Trusted_Connection=True");
}
}
我正在使用 Visual Studio 2022 预览版和 .NET 6 SDK。
这里我创建了一个 2 层的 webAPI 项目。 api 项目 (Bgvsystem.webAPI) class 图书馆 (BgvSystem.Persistance)
NuGet 包-
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.0-rc.1.21452.10
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.0-rc.1.21452.10
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 6.0.0-rc.1.21464.1
当我尝试使用脚手架添加控制器时,出现以下错误
There was an error running the selected code generator: unable to resolve service for type 'microsoft.entityframeworkcore.dbcontextoption.. While attempting to activate Dbcontext in .net 6 and visual studio 2022 preview
如何解决这个问题?请帮忙解决这个问题。
您需要将 DBContext 添加到 startup.cs 文件中的服务集合或作为构造函数 BGvSystemContext class.
传递折腾了3天,终于找到错误并修复了。
实际上我必须将连接字符串放在 MyApp.Persistance.DbContextClass 中,如下所示
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=DESKTOP-SV8GPJ2\SQLEXPRESS;Initial Catalog=StarterAppDB;Persist Security Info=True;User ID=sa;Password=Admin@1234");
}
}
然后它运行良好。
如果是 .NET 6
你能这样试试吗
Program.cs
builder.Services.AddDbContext<YourDbContext>(options =>
options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Server=YourServer; Database=YourDb; Integrated Security=true; MultipleActiveResultSets=true; Trusted_Connection=True"
}
YourDbContext.cs
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=YourServer; Database=YourDb; Integrated Security=true; MultipleActiveResultSets=true; Trusted_Connection=True");
}
}