add-Migration Error 没有为此 DbContext 配置数据库提供程序

add-Migration Error No database provider has been configured for this DbContext

我正在尝试将迁移添加到 DbContext,

add-migration initial -verbose

我收到错误

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 object in its constructor and passes it to the base constructor for DbContext.

我的解决方案中有两个 .netcore class 库项目和 netcore 单元测试项目

  1. 域(Poco Classess)
  2. 存储库(.Net Core 2.1,EntitiFrameworkCore 2.1.4)
  3. 存储库测试

这是我的 DataContext Class

 public class DataContext:DbContext
    {
        public DataContext(DbContextOptions<DataContext> option) : base(option)
        {

        }

        public DataContext()
        {

        }

    public DbSet<User> User { get; set; }
    public DbSet<Cart> Cart { get; set; }
    public DbSet<CatalogItem> CatalogItem { get; set; }
 }

带有 DbContextOptions 对象的构造函数已经存在。

可能是什么问题?

这里是测试项目中的 class。

 public class CustomerRepositoryIntegrationTest
    {
        [Fact]
        public void should_add_customer()
        {
            //Arrange
            var option = new DbContextOptionsBuilder<DataContext>()
            .UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=ecommerce;Integrated Security=SSPI").Options;

            //Act
            using (DataContext dataConext = new DataContext(option))
            {

                dataConext.Database.Migrate();
                customer actual = new Customer()
                dataConext.Customer.Add(actual);
                dataConext.SaveChanges();

                var expected = dataConext.Customer.FirstOrDefault();

                //Assert
                expected.Should().BeEquivalentTo(expected);
            }


            //Assert
        }
    }

创建一个 class 在测试项目中实现 IDesignTimeDbContextFactory 并将测试项目设置为启动项目

public class TemporaryDataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
    public DataContext CreateDbContext(string[] args)
    {
        var option = new DbContextOptionsBuilder<DataContext>()
        .UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=IbReport;Integrated Security=SSPI").Options;
        return new DataContext(option);
    }
}