简体   繁体   English

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

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

i am trying to add migration to a DbContext ,我正在尝试将迁移添加到 DbContext ,

add-migration initial -verbose

I get Error我收到错误

No database provider has been configured for this DbContext.尚未为此 DbContext 配置数据库提供程序。 A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider.可以通过覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供者上使用 AddDbContext 来配置提供者。 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.如果使用 AddDbContext,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。

i have two .netcore class library project and and ,netcore unit test project in my solution我的解决方案中有两个 .netcore 类库项目和 ,netcore 单元测试项目

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

here is my DataContext Class这是我的 DataContext 类

 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; }
 }

a constructor with DbContextOptions object is already there .带有 DbContextOptions 对象的构造函数已经存在。

what could be possibly the problem ?可能是什么问题?

and here is a class in test project.这是测试项目中的一个类。

 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
        }
    }

create a class that implement IDesignTimeDbContextFactory in test project and set test project as startup project在测试项目中创建一个实现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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加迁移错误:尚未为此DbContext配置数据库提供程序 - Add migration error: No database provider has been configured for this DbContext 初始数据库迁移:没有为此 DbContext 配置数据库提供程序 - Initial db migration: No database provider has been configured for this DbContext 无法解决此错误,“尚未为此 DbContext 配置数据库提供程序” - Cannot solve this error, "No database provider has been configured for this DbContext" 错误:System.InvalidOperationException:没有为此 DbContext 配置数据库提供程序 - ERROR: System.InvalidOperationException: No database provider has been configured for this DbContext 错误提示。没有为此 DbContext 配置数据库提供程序 - Error saying.No database provider has been configured for this DbContext 尚未为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext Azure KeyVault for DBContext:“尚未为此DbContext配置数据库提供程序” - Azure KeyVault for DBContext: “No database provider has been configured for this DbContext” InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过覆盖DbContext来配置提供程序 - InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext 没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 - No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM