实体框架 4.1“代码优先” Database.Delete 之后不会再次调用 SetInitializer
首先尝试使用 EF 4.1 代码进行一些单元测试。我有我的实时数据库(SQL Server)和我的单元测试数据库(Sql CE)。在与 EF、Sql CE 4.0 和事务支持战斗(并失败)后,我决定运行测试的最简单方法是:
- 创建 Db
- 运行测试
- 删除 Db
- 冲洗并重复
我有我的 [Setup] 和 [TearDown] 功能:
[SetUp]
public void Init()
{
System.Data.Entity.Database.SetInitializer(new MyTestContextInitializer());
_dbContext = ContainerFactory.Container.GetInstance<IContext>();
_testConnection = _dbContext.ConnectionString;
}
[TearDown]
public void Cleanup()
{
_dbContext.Dispose();
System.Data.Entity.Database.Delete(_testConnection);
}
问题是System.Data.Entity.Database.SetInitializer 在第一次测试后不会调用 MyTestContextInitializer。
因此,第二个测试失败:
System.Data.EntityException: 底层提供程序在打开时失败。
----> System.Data.SqlServerCe.SqlCeException : 找不到数据库文件。 检查数据库路径
是否有任何指针
Trying to do some unit testing with EF 4.1 code first. I have my live db (SQL Server) and my unit test DB( Sql CE). After fighting (and losing) with EF, Sql CE 4.0 and Transaction support I decided the simplest way to run my test was to:
- Create Db
- Run Test
- Delete Db
- Rinse and repeat
I have my [Setup] and [TearDown] functions:
[SetUp]
public void Init()
{
System.Data.Entity.Database.SetInitializer(new MyTestContextInitializer());
_dbContext = ContainerFactory.Container.GetInstance<IContext>();
_testConnection = _dbContext.ConnectionString;
}
[TearDown]
public void Cleanup()
{
_dbContext.Dispose();
System.Data.Entity.Database.Delete(_testConnection);
}
Issue is that System.Data.Entity.Database.SetInitializer does not call MyTestContextInitializer after the first test.
Hence the 2nd test then fails with:
System.Data.EntityException : The
underlying provider failed on Open.
----> System.Data.SqlServerCe.SqlCeException
: The database file cannot be found.
Check the path to the database
TIA for any pointers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过手动调用“InitializeDatabase”解决了这个问题。就像这样:
我认为这可能是 EF 4.1 RC 的一个错误。
I got around this by calling 'InitializeDatabase' manually. Like so:
I think it may be a bug with EF 4.1 RC.
这不是一个错误,
只有在 AppDomain 中第一次创建上下文时才会调用设置的初始化程序。因此,由于您在单个 AppDomain 中运行所有测试,因此仅在运行第一个测试时才会调用它。
It's not a bug, the initializer set with
is only called when the context is created for the first time in the AppDomain. Hence, since you're running all your tests in a single AppDomain, it's only called when the first test is ran.
我花了几乎一天的时间才找出导致我奇怪的单元测试行为的原因:数据库连接保持打开状态或者数据库不是在每个新测试中创建的。我到处搜索原因的根源:MSTest(没有管理员权限或文件的工作副本以某种方式删除了?)、SQL Server Express/CE(登录失败?)、Unity(未处理对象?)或 Entity Framework(没有正确的文件)数据库初始化?)。原来是EF。非常感谢您的回答!
It took me almost a day to find out what caused my strange unittest behaviour: the database connection stayed open or the database was not created with a every new test. I searched everywhere for the root of the cause: MSTest (no Admin rights or where working copies of files somehow deleted?), SQL Server Express/CE (login failure?), Unity (objects not disposed?) or Entity Framework (no proper database initialization?). It turned out to be EF. Thanks a lot for the answer!