迁移如何将连接字符串传递到库项目 (.netStandard)
Migration How to pass Connection String into Library Project (.netStandard)
我的解决方案有 3 个项目:
- 实体(包括 Dbcontext,...),目标框架 .NetStandard 1.4,项目类型库
- WebApi
WebUi
我想创建迁移到实体项目中的函数。在实体项目中,我有一个 class ApplicationContextFactory
public class ApplicationContextFactory : IDbContextFactory<ApplicationContext>
{
public ApplicationContext Create(DbContextFactoryOptions options)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationContext>();
return new ApplicationContext(optionsBuilder.Options);
}
}
我想将连接字符串从 WebAPI 传递到实体项目
我的启动 Class (WebAPI)
services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));
但是当我使用命令 Add-Migration InitialMigration 时出现错误:
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
我解决了这个问题。因为在我的解决方案中有 2 个项目是 WebUI 和 WebAPI,我只是将包含连接字符串的 WebAPI 设置为启动项目并且它运行良好
我的解决方案有 3 个项目:
- 实体(包括 Dbcontext,...),目标框架 .NetStandard 1.4,项目类型库
- WebApi
WebUi
我想创建迁移到实体项目中的函数。在实体项目中,我有一个 class ApplicationContextFactory
public class ApplicationContextFactory : IDbContextFactory<ApplicationContext> { public ApplicationContext Create(DbContextFactoryOptions options) { var optionsBuilder = new DbContextOptionsBuilder<ApplicationContext>(); return new ApplicationContext(optionsBuilder.Options); } }
我想将连接字符串从 WebAPI 传递到实体项目 我的启动 Class (WebAPI)
services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));
但是当我使用命令 Add-Migration InitialMigration 时出现错误:
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
我解决了这个问题。因为在我的解决方案中有 2 个项目是 WebUI 和 WebAPI,我只是将包含连接字符串的 WebAPI 设置为启动项目并且它运行良好