【发布时间】:2018-02-25 11:11:14
【问题描述】:
在 Startup.cs 中的 WebApi (.NET Core 2.0 + EF Core) 项目中
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<MyContext>(options =>
options.UseSqlServer(_config["ConnectionStrings:MyConnectionString"]));
services.AddMvc();
}
上下文:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{ }
public MyContext()
{
}
public DbSet<Employee> Employees { get; set; }
}
当我调用 WebApi 时没有问题。
但在我的集成测试中,我想这样做:
[Fact]
void TestMethod()
{
var context = new MyContext();
var service = new MyService(context);
var result = service.GetAll();//Error here
Assert.True(result.Count() > 0);
}
我收到此错误:
没有为此 DbContext 配置数据库提供程序。一种 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 方法或通过在应用程序服务提供者上使用 AddDbContext
如何实例化上下文并指定要使用的连接字符串?
【问题讨论】:
标签: c# entity-framework asp.net-core .net-core entity-framework-core