0

我在我的 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);
                }
            }
        }

知道如何从代码中实现这一点(我正在尝试自动化测试)吗?

4

1 回答 1

1

在服务器资源管理器上,右键单击您与该数据库的数据连接并选择关闭连接。

于 2013-05-05T21:54:03.433 回答