asp.net-mvc 无法删除数据库,因为它当前正在使用 MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17095472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:56:32  来源:igfitidea点击:

Cannot drop database because it is currently in use MVC

asp.net-mvcsql-server-2008

提问by SantasNotReal

I'm brand new to using MVC, and I'm trying to use an initializer to initialize data into my DB when the application is first started. Here is what I've got in Global.asax.cs:

我是使用 MVC 的新手,我试图在应用程序首次启动时使用初始化程序将数据初始化到我的数据库中。这是我在 Global.asax.cs 中得到的:

 System.Data.Entity.Database.SetInitializer(new MyAppInitializer());
 MyAppContext db = new MyAppContext();
 db.Database.Initialize(true);

In Web.config, here is my connection string:

在 Web.config 中,这是我的连接字符串:

<connectionStrings>
  <add name="MyAppContext"
  connectionString="data source= MyServer; Integrated Security=True; database=MyDatabase"
  providerName="System.Data.SqlClient"/>

This is using MS SQL 2008 R2. My Initializer looks like this:

这是使用 MS SQL 2008 R2。我的初始化程序如下所示:

public class MyAppInitializer : DropCreateDatabaseAlways<MyAppContext>
{
    protected override void Seed(MyAppContext context)
    {
        var organizations = new List<Organizations>
        {
            new Organizations { OrgName = "AT", OrgPhone = 5093333433, OrgOfficeLocation = "ITB", OrgPointOfContact = "Tony", OrgIsActive = 1 },
            new Organizations { OrgName = "Libraries", OrgPhone = 5093331122, OrgOfficeLocation = "Holland-Terrell", OrgPointOfContact = "Herald", OrgIsActive = 1 }
        };
        organizations.ForEach(s => context.Organizations.Add(s));
        context.SaveChanges();

I made sure I closed my connection to the server and database in SQL Server Management Studio, but multiple people have access to this DB, although none should be using it right now. How can I get it so I can initialize this data in my DB? Thanks!

我确保在 SQL Server Management Studio 中关闭了与服务器和数据库的连接,但是多人可以访问此数据库,尽管现在应该没有人使用它。我怎样才能得到它以便我可以在我的数据库中初始化这些数据?谢谢!

Edit: I've already got the DB created on the server, but it is completely empty (no tables, procedures, etc). Would this cause an issue?

编辑:我已经在服务器上创建了数据库,但它完全是空的(没有表、过程等)。这会导致问题吗?

回答by TwoPea

I faced a similar issue today when using MVC codefirst. After 20 mins of trying out various stuff I noticed that, the "Server Explorer" tab in Visual Studio had a connection open to my database. After I "closed" the connection in the server explorer tab of visual studio, the code was able to run and automatically recreate the database.

我今天在使用 MVC codefirst 时遇到了类似的问题。在尝试各种东西 20 分钟后,我注意到,Visual Studio 中的“服务器资源管理器”选项卡打开了到我的数据库的连接。在 Visual Studio 的服务器资源管理器选项卡中“关闭”连接后,代码能够运行并自动重新创建数据库。

回答by Sam

In SSMS run something like this...

在 SSMS 中运行这样的东西......

USE master -- be sure that you're not on MYDB
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE MYDB;

回答by Bob Mack

VS2015 close DB connectionAs described by Vardhini... close DB connection in Server Explorer.

VS2015 关闭数据库连接如 Vardhini 所述...在服务器资源管理器中关闭数据库连接。

回答by Brice

In the SSMS "Delete" window make sure that "Close existing connections" is checked.

在 SSMS“删除”窗口中,确保选中“关闭现有连接”。

回答by Vardhini

Closing all existing connections of the database in visual studio server explorer and SQLManagement studio solved the problem for me.

在 Visual Studio Server Explorer 和 SQLManagement Studio 中关闭数据库的所有现有连接为我解决了这个问题。

回答by Sebastian Xawery Wi?niowiecki

My observations are:

我的观察是:

  1. Cannot be logged in to application
  2. Cannot be connected to db with Server Explorer
  3. Cannot be connected with SSMS
  1. 无法登录应用程序
  2. 无法使用服务器资源管理器连接到数据库
  3. 无法与 SSMS 连接

Then the application rebuild DB even when Database.SetInitializer<DbContext>(new DbInitializer());is in public DbContext();- NOT as other answears stand to put it into Application_Start();

然后应用程序重建数据库,即使Database.SetInitializer<DbContext>(new DbInitializer());是在public DbContext();- 不像其他人说的那样把它放进去Application_Start();