visual studio 2022 .NET 6 脚手架在 DbContext 位于单独的项目中时不起作用
visual studio 2022 .NET 6 Scaffolding doesn't work when DbContext is in a separate project
我正在设置我的第一个 .NET 6 MVC 站点,并且遇到了第一个障碍。
我在解决方案中有 3 个项目。
域:包含实体
基础架构:具有应用程序数据库上下文
WebUi:包含网页界面
添加迁移和更新数据库工作正常,但是当我尝试构建控制器时,出现以下错误:
There was an error running the selected code generator:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions’ While attempting to activate
然后在其他 class 库项目中命名 DbContext。
几个月前我发现其他人也遇到过类似的问题,但除了将所有内容都纳入同一个项目之外,没有人有任何解决方案。
有没有人遇到过这个问题并找到了解决办法?
首先确保您在与上下文相同的项目中拥有正确的包。您将需要以下内容。
microsoft.aspnetcore.diagnostics.entityframeworkcore
microsoft.entityframeworkcore.sqlserver
还要确保您在上下文中使用的是硬编码选项生成器连接,而不是如下所示的名称参数。请记住在发布您的更改之前将其更改回来。
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
//optionsBuilder.UseSqlServer
//("Name=ConnectionStrings:[AppSettingsConnectionName]");
// Use this OB to scaffold
optionsBuilder.UseSqlServer
(@"Data Source=[Server];Initial Catalog=[DBName];");
}
}
终于找到了对我有用的东西:
您可以在与 ApplicationDbContext class 相同的文件夹中创建一个 dbcontext 工厂 class。该工厂 class 在设计时创建 ApplicationDbContext 并且脚手架运行正确
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("Server=(localdb)\MSSQLLocalDB;Database=EcommerceDb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new ApplicationDbContext(optionsBuilder.Options);
}
}
致谢:https://github.com/dotnet/Scaffolding/issues/1765#issuecomment-1058674843
我正在设置我的第一个 .NET 6 MVC 站点,并且遇到了第一个障碍。
我在解决方案中有 3 个项目。 域:包含实体 基础架构:具有应用程序数据库上下文 WebUi:包含网页界面
添加迁移和更新数据库工作正常,但是当我尝试构建控制器时,出现以下错误:
There was an error running the selected code generator:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions’ While attempting to activate
然后在其他 class 库项目中命名 DbContext。
几个月前我发现其他人也遇到过类似的问题,但除了将所有内容都纳入同一个项目之外,没有人有任何解决方案。
有没有人遇到过这个问题并找到了解决办法?
首先确保您在与上下文相同的项目中拥有正确的包。您将需要以下内容。
microsoft.aspnetcore.diagnostics.entityframeworkcore microsoft.entityframeworkcore.sqlserver
还要确保您在上下文中使用的是硬编码选项生成器连接,而不是如下所示的名称参数。请记住在发布您的更改之前将其更改回来。
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
//optionsBuilder.UseSqlServer
//("Name=ConnectionStrings:[AppSettingsConnectionName]");
// Use this OB to scaffold
optionsBuilder.UseSqlServer
(@"Data Source=[Server];Initial Catalog=[DBName];");
}
}
终于找到了对我有用的东西: 您可以在与 ApplicationDbContext class 相同的文件夹中创建一个 dbcontext 工厂 class。该工厂 class 在设计时创建 ApplicationDbContext 并且脚手架运行正确
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("Server=(localdb)\MSSQLLocalDB;Database=EcommerceDb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new ApplicationDbContext(optionsBuilder.Options);
}
}
致谢:https://github.com/dotnet/Scaffolding/issues/1765#issuecomment-1058674843