我在我的 EF codefirst Seed 方法中尝试以下操作:
protected override void Seed(TestDbContext context)
{
SqlConnection.ClearAllPools();
context.Database.Delete();
context.Database.CreateIfNotExists();
if (!WebMatrix.WebData.WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}
这失败并出现错误“无法删除,因为数据库当前正在使用中”。它似乎只发生在我运行我的 Api 项目之后,该项目初始化了它自己与成员表的连接:
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
Database.SetInitializer<UsersContext>(null);
try
{
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
if (!WebMatrix.WebData.WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId",
"UserName", autoCreateTables: true);
}
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
知道如何从代码中实现这一点(我正在尝试自动化测试)吗?