Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加迁移错误尚未为此DbContext配置数据库提供程序_C#_Entity Framework_Ef Core 2.1 - Fatal编程技术网

C# 添加迁移错误尚未为此DbContext配置数据库提供程序

C# 添加迁移错误尚未为此DbContext配置数据库提供程序,c#,entity-framework,ef-core-2.1,C#,Entity Framework,Ef Core 2.1,我正在尝试将迁移添加到DbContext中 add-migration initial -verbose 我犯了一个错误 尚未为此DbContext配置任何数据库提供程序。A. 可以通过重写DbContext.onconfig来配置提供程序 方法或在应用程序服务提供程序上使用AddDbContext。 如果使用了AddDbContext,那么还要确保您的DbContext类型 在其构造函数中接受DbContextOptions对象,然后 将其传递给DbContext的基构造函数 我的解决方案中

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

add-migration initial -verbose
我犯了一个错误

尚未为此DbContext配置任何数据库提供程序。A. 可以通过重写DbContext.onconfig来配置提供程序 方法或在应用程序服务提供程序上使用AddDbContext。 如果使用了AddDbContext,那么还要确保您的DbContext类型 在其构造函数中接受DbContextOptions对象,然后 将其传递给DbContext的基构造函数

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

  • 域(Poco类)
  • 存储库(.Net Core 2.1,EntitiFrameworkCore 2.1.4)
  • 安置试验
  • 这是我的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; }
     }
    
    公共类DataContext:DbContext
    {
    公共DataContext(DbContextOptions选项):基本(选项)
    {
    }
    公共数据上下文()
    {
    }
    公共数据库集用户{get;set;}
    公共数据库集购物车{get;set;}
    公共数据库集目录项{get;set;}
    }
    
    已存在具有DbContextOptions对象的构造函数

    可能是什么问题

    这是一个测试项目中的类

     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
            }
        }
    
    公共类CustomerRepositoryIntegrationTest
    {
    [事实]
    公共作废应添加客户()
    {
    //安排
    var option=new DbContextOptionsBuilder()
    .UseSqlServer(@“数据源=(LocalDb)\MSSQLLocalDB;数据库=电子商务;集成安全=SSPI”)。选项;
    //表演
    使用(DataContext dataConext=newdatacontext(选项))
    {
    dataConext.Database.Migrate();
    实际客户=新客户()
    dataConext.Customer.Add(实际);
    dataConext.SaveChanges();
    var expected=dataConext.Customer.FirstOrDefault();
    //断言
    应为.Should().beequivalento(应为);
    }
    //断言
    }
    }
    
    创建在测试项目中实现的类,并将测试项目设置为启动项目

    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);
        }
    }
    
    公共类TemporaryDataContextFactory:IDesignTimeDbContextFactory
    {
    公共DataContext CreateDbContext(字符串[]args)
    {
    var option=new DbContextOptionsBuilder()
    .UseSqlServer(@“数据源=(LocalDb)\MSSQLLocalDB;数据库=IbReport;集成安全性=SSPI”)。选项;
    返回新的DataContext(选项);
    }
    }
    
    嗯,您似乎没有按照错误消息所告诉的那样配置DbContext。您的上下文配置在哪里?@CamiloTerevinto它在我的测试项目中。我只是更新了代码,问题到底出了什么问题?为什么投反对票呢?
    addmigration
    命令不知道您的单元测试,因此对于该命令,您没有配置上下文。解决这个问题最简单的方法是覆盖
    onconfigurang
    并配置上下文(如果没有发生)(就像在测试中发生的那样)@CamiloTerevinto我应该在测试项目中这样做吗?太棒了!这比使用一次性的控制台应用要好得多。在.NETCore中,我从我的上下文项目目录运行了
    dotnetef迁移添加InitialCreate--startupproject
    ,一切都很顺利。非常感谢你。