迁移如何将连接字符串传递到库项目(.netStandard)

时间:2017-09-28 03:49:31

标签: c# entity-framework .net-core ef-migrations .net-standard

我的解决方案有3个项目:

  • 实体(包括Dbcontext,...),目标框架.NetStandard 1.4,项目类型库
  • 的WebAPI
  • WebUI中

  • 我想创建函数迁移到Entity Project。在Entity项目中,我有一个ApplicationContextFactory类

    public class ApplicationContextFactory : IDbContextFactory<ApplicationContext>
    {
        public ApplicationContext Create(DbContextFactoryOptions options)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationContext>();
            return new ApplicationContext(optionsBuilder.Options);
        }
    }
    

我想将连接字符串从WebAPI传递到Entity项目 我的StartUp类(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.

1 个答案:

答案 0 :(得分:2)

我解决了这个问题。因为在我的解决方案中有2个项目是WebUI和WebAPI,我只是设置为WebAPI的启动项目,它包含连接字符串并且运行良好