C# 如何将DbContext添加到MSTest项目中?
我试图测试一些使用实体框架的代码,但我不知道如何从单独的MSTest项目中引用EF上下文类。两个项目都在同一个解决方案中 无法将lambda表达式转换为类型“DbContextOptions”,因为它不是委托类型 在我的测试案例中:C# 如何将DbContext添加到MSTest项目中?,c#,mstest,dbcontext,C#,Mstest,Dbcontext,我试图测试一些使用实体框架的代码,但我不知道如何从单独的MSTest项目中引用EF上下文类。两个项目都在同一个解决方案中 无法将lambda表达式转换为类型“DbContextOptions”,因为它不是委托类型 在我的测试案例中: [TestClass] public class GreenCardUserTest { [TestMethod] public void TestAddUser() { // REFERENCE TO OTHER PROJE
[TestClass]
public class GreenCardUserTest
{
[TestMethod]
public void TestAddUser()
{
// REFERENCE TO OTHER PROJECT. WORKS FINE
AppUserViewModel a = new AppUserViewModel();
//LIKELY INCORRECT attempt to duplicate code from Startup.cs in other project
using (GreenCardContext _gc = new GreenCardContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnection"))))
{
new GCLandingUserModel().AddUser(a,_gc);
}
}
}
摘自main project Startup.cs(运行良好):
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“MyConnection”));
您从Startup.cs
获得的代码使用委托告诉您的应用程序如何在运行时构建DbContext
但是,在测试中,实际上需要提供DbContextOptions
的实例,而不仅仅是一个委托。为此,可以使用DbContextOptionsBuilder
:
var options = new DbContextOptionsBuilder<GreenCardContext>()
.UseSqlServer(Configuration.GetConnectionString("MyConnection"))
.Options;
using (GreenCardContext _gc = new GreenCardContext(options))
{
new GCLandingUserModel().AddUser(a,_gc);
}
var options=new DbContextOptionsBuilder()
.UseSqlServer(Configuration.GetConnectionString(“MyConnection”))
.选择;
使用(GreenCardContext _gc=新的GreenCardContext(选项))
{
新的GCLandingUserModel().AddUser(a,_gc);
}
此外,如果您坚持对DbConext进行单元测试,您可能希望研究使用InMemoryDatabase,这样您就不需要在测试中使用开放的SQL连接。有关更多详细信息,请参阅。您从
启动.cs
获得的代码正在使用委托告诉您的应用程序如何在运行时构建DbContext
但是,在测试中,实际上需要提供DbContextOptions
的实例,而不仅仅是一个委托。为此,可以使用DbContextOptionsBuilder
:
var options = new DbContextOptionsBuilder<GreenCardContext>()
.UseSqlServer(Configuration.GetConnectionString("MyConnection"))
.Options;
using (GreenCardContext _gc = new GreenCardContext(options))
{
new GCLandingUserModel().AddUser(a,_gc);
}
var options=new DbContextOptionsBuilder()
.UseSqlServer(Configuration.GetConnectionString(“MyConnection”))
.选择;
使用(GreenCardContext _gc=新的GreenCardContext(选项))
{
新的GCLandingUserModel().AddUser(a,_gc);
}
此外,如果您坚持对DbConext进行单元测试,您可能希望研究使用InMemoryDatabase,这样您就不需要在测试中使用开放的SQL连接。有关更多详细信息,请参阅。您需要做的是: 1) 将测试项目中的引用添加到上下文的项目中(如果尚未添加) 2) 将对实体框架的引用添加到测试项目中 3) 将appconfig添加到测试项目中,并在其上设置EntityFramework配置。您的测试将从自己的配置中读取配置,而不是从应用程序的配置中读取。非常有用,例如,在测试中使用dblocal和codefirst,在运行sqlserver时使用:)
你已经做了一些,我认为你缺少的是第三点:)你必须做的是: 1) 将测试项目中的引用添加到上下文的项目中(如果尚未添加) 2) 将对实体框架的引用添加到测试项目中 3) 将appconfig添加到测试项目中,并在其上设置EntityFramework配置。您的测试将从自己的配置中读取配置,而不是从应用程序的配置中读取。非常有用,例如,在测试中使用dblocal和codefirst,在运行sqlserver时使用:)
您已经做了一些,我认为您缺少的是第三点:)我建议使用InMemoryDatabase: 在测试类中,使用[TestInitialize]设置虚拟数据库:
[TestClass]
public class GreenCardUserTest
{
private readonly context;
[TestInitialize]
public Setup()
{
DbContextOptions<GreenCardContext> options;
var builder = new DbContextOptionsBuilder<GreenCardContext>();
builder.UseInMemoryDatabase();
var options = builder.Options;
context = new GreenCardContext(options);
}
[TestMethod]
public void TestAddUser()
{
// user context here...
}
}
[TestClass]
公共类GreenCardUserTest
{
私有只读上下文;
[测试初始化]
公共设置()
{
DbContextOptions选项;
var builder=new DbContextOptionsBuilder();
builder.UseInMemoryDatabase();
var options=builder.options;
上下文=新的绿卡上下文(选项);
}
[测试方法]
公共无效TestAddUser()
{
//用户上下文在此。。。
}
}
我建议使用InMemoryDatabase:
在测试类中,使用[TestInitialize]设置虚拟数据库:
[TestClass]
public class GreenCardUserTest
{
private readonly context;
[TestInitialize]
public Setup()
{
DbContextOptions<GreenCardContext> options;
var builder = new DbContextOptionsBuilder<GreenCardContext>();
builder.UseInMemoryDatabase();
var options = builder.Options;
context = new GreenCardContext(options);
}
[TestMethod]
public void TestAddUser()
{
// user context here...
}
}
[TestClass]
公共类GreenCardUserTest
{
私有只读上下文;
[测试初始化]
公共设置()
{
DbContextOptions选项;
var builder=new DbContextOptionsBuilder();
builder.UseInMemoryDatabase();
var options=builder.options;
上下文=新的绿卡上下文(选项);
}
[测试方法]
公共无效TestAddUser()
{
//用户上下文在此。。。
}
}
我一直得到:\'Configuration'不包含'GetConnectionString'的定义。
我刚刚从您最初发布的代码中复制了该部分。也许您想使用ConfigurationManager
而不是Configuration
?谢谢,我最终传递了一个硬编码的conn字符串,因为我的测试项目无法访问我真正的项目appsettings.jsonI继续得到:\'Configuration'不包含'GetConnectionString'的定义。
我刚刚从您最初发布的代码中复制了该部分。也许您打算使用ConfigurationManager
而不是Configuration
?谢谢,我最终传递了一个硬编码的conn字符串,因为我的测试项目无法访问我的真实项目appsettings.json