CREATE DATABASE语句不允许多语句事务中 [英] CREATE DATABASE statement not allowed within multi-statement transaction
问题描述
我试图建立一个快速测试和删除每次运行时重新创建一个数据库。我有以下几点:
I'm trying to build a quick test that deletes and recreates a database every time it runs. I have the following:
[TestClass]
public class PocoTest
{
private TransactionScope _transactionScope;
private ProjectDataSource _dataSource;
private Repository _repository = new Repository();
private const string _cstring = "Data Source=.;Initial Catalog=test_db;Trusted_Connection=True";
[TestInitialize]
public virtual void TestInitialize()
{
_dataSource = new ProjectDataSource(_cstring);
_dataSource.Database.Delete();
_dataSource.Database.CreateIfNotExists();
_transactionScope = new TransactionScope();
}
[TestMethod]
public void TestBasicOperations()
{
var item = _repository.AddItem(new Item(){Details = "Test Item"});
// AddItem makes a call through the data context to add a set and then calls datacontext.SaveChanges()
}
[TestCleanup]
public void TestCleanup()
{
// rollback
if (_transactionScope != null)
{
_transactionScope.Dispose();
}
}
然而,当我运行测试,我得到以下错误
However when I run the test I get the following error:
结果消息:试验方法
Project.Repository.UnitTests.PocoTest.TestBasicOperations扔
例外:系统。 Data.SqlClient.SqlException:创建多语句事务中不允许DATABASE
语句
Result Message: Test method Project.Repository.UnitTests.PocoTest.TestBasicOperations threw exception: System.Data.SqlClient.SqlException: CREATE DATABASE statement not allowed within multi-statement transaction.
ProjectDataSource是在这里:
ProjectDataSource is here:
public class ProjectDataSource : DbContext, IProjectDataSource
{
public ProjectDataSource() : base("DefaultConnection")
{
}
public ProjectDataSource(string connectionString) : base(connectionString)
{
}
public DbSet<Set> Sets { get; set; }
}
存储库:
Repository:
public class Repository : IRepository
{
private readonly ProjectDataSource _db = new ProjectDataSource();
public Item AddItem(Item item)
{
_db.Items.Add(item);
_db.SaveChanges();
return item;
}
}
这是怎么回事?
Why is this happening?
此外 - 如果它使任何区别 - 如果我注释掉TestMethod的该行的AddItem不会发生错误。
Also - if it makes any difference - the error doesn't occur if I comment out the AddItem line in TestMethod.
推荐答案
在万一别人运行到这个问题:
In case anyone else runs into this issue:
在我的资源库类,我有什么常见的另一个定义贴上了的DbContext - ProjectDataSource。这意味着一个方面是在我的测试类创建的,而另一个是在我的仓库对象创建的。发送ConnectionString,以我的回购类解决了这个问题:
In my Repository class, I have another definition of what's commonly labeled a "dbContext" - ProjectDataSource. This means that one context was created in my test class, while another was created in my Repository object. Sending the connectionstring to my repo class solved the problem:
在存储库:
public class Repository : IRepository
{
private readonly ProjectDataSource _db;
public Repository(string connectionString)
{
_db = new ProjectDataSource(connectionString);
}
public Repository()
{
_db = new ProjectDataSource();
}
这是我的测试:
private TransactionScope _transactionScope;
private Repository _repository;
private ProjectDataSource _dataSource;
private const string _connectionString = "Data Source=.;Initial Catalog=test_db;Trusted_Connection=True";
[TestInitialize]
public virtual void TestInitialize()
{
_repository = new Repository(_connectionString);
_dataSource = new ProjectDataSource(_connectionString);
_dataSource.Database.Delete();
_dataSource.Database.CreateIfNotExists();
_transactionScope = new TransactionScope();
}
这篇关于CREATE DATABASE语句不允许多语句事务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!