使用实体框架删除数据库

时间:2021-12-19 09:51:35

I'm trying to remove a database form my application using entity framework. The code I use is the following:

我正在尝试使用实体框架从我的应用程序中删除数据库。我使用的代码如下:

using (var dbContext = container.Resolve<ApplicationDbContext>())
    {
      dbContext.Database.Delete();
    }

According to msdn this should work but nothing happens.

根据msdn,这应该工作但没有任何反应。

the dbContext is registered using ContainerControlledLifetimeManager and should be the same instance used to create the DB.

dbContext使用ContainerControlledLifetimeManager注册,并且应该与用于创建数据库的实例相同。

2 个解决方案

#1


2  

Adding, updating and deleting instances of entity types needs dbContext.SaveChanges() to reflect changes.

添加,更新和删除实体类型的实例需要dbContext.SaveChanges()来反映更改。

However dbContext.Database.Delete() does not need dbContext.SaveChanges().

但是dbContext.Database.Delete()不需要dbContext.SaveChanges()。

If you open connection for example from Sql Management Studio to your database and try to dbContext.Database.Delete() then you receive Cannot drop database "dbContext" because it is currently in use. If you restart you sql server, you drop those connections and then retry dbContext.Database.Delete() you successfully drop database.

如果您打开例如从Sql Management Studio到数据库的连接并尝试dbContext.Database.Delete(),那么您将收到无法删除数据库“dbContext”,因为它当前正在使用中。如果重新启动sql server,则删除这些连接,然后重试dbContext.Database.Delete()以成功删除数据库。

Last thing is refresh database list in Sql Management Studio in order to see that database is not there any more.

最后一件事是在Sql Management Studio中刷新数据库列表,以便看到该数据库不再存在。

Testing with this code snippet:

使用此代码段进行测试:

using (var dbContext = new dbContext())
{
    dbContext.Database.Delete();
}

#2


1  

After @moguzalp and this (MSDN Database.Delete Function), I came with a solution for my case:

在@moguzalp和这个(MSDN Database.Delete函数)之后,我为我的案例提供了一个解决方案:

using System.Data.Entity;

Database.Delete("connectionStringOrName");

In my case I was trying to Recreate a mssqllocalDb database for test purposes. But whenever I used the same DbContext (or an immediately new one, disposing the first and opening another), it looked like the database was still up when I tried to create it.

在我的情况下,我试图重新创建一个mssqllocalDb数据库用于测试目的。但每当我使用相同的DbContext(或者立即使用新的DbContext,处理第一个并打开另一个)时,当我尝试创建数据库时,它看起来仍然是数据库。

Bad Example: (x)

不好的例子:(x)

public static void RecreateDatabase(string schemaScript)
{   
    using (DbContext context = new DbContext("connectionStringName"))
    {
        context.Database.Delete(); // Delete returns true, but...
        Database database = context.Database;
        database.Create(); // Database already exists!
        database.ExecuteSqlCommand(schemaScript);
    }
}

Working example: (/)

工作实例:(/)

public static void RecreateDatabase(string schemaScript)
{   
    Database.Delete(); // Opens and disposes its own connection

    using (DbContext context = new DbContext("connectionStringName")) // New connection
    {
        Database database = context.Database;
        database.Create(); // Works!
        database.ExecuteSqlCommand(schemaScript);
    }
}

Context: I'm using this on an [AssemblyInitialize] function to test my ModelContexts

上下文:我在[AssemblyInitialize]函数中使用它来测试我的ModelContexts

#1


2  

Adding, updating and deleting instances of entity types needs dbContext.SaveChanges() to reflect changes.

添加,更新和删除实体类型的实例需要dbContext.SaveChanges()来反映更改。

However dbContext.Database.Delete() does not need dbContext.SaveChanges().

但是dbContext.Database.Delete()不需要dbContext.SaveChanges()。

If you open connection for example from Sql Management Studio to your database and try to dbContext.Database.Delete() then you receive Cannot drop database "dbContext" because it is currently in use. If you restart you sql server, you drop those connections and then retry dbContext.Database.Delete() you successfully drop database.

如果您打开例如从Sql Management Studio到数据库的连接并尝试dbContext.Database.Delete(),那么您将收到无法删除数据库“dbContext”,因为它当前正在使用中。如果重新启动sql server,则删除这些连接,然后重试dbContext.Database.Delete()以成功删除数据库。

Last thing is refresh database list in Sql Management Studio in order to see that database is not there any more.

最后一件事是在Sql Management Studio中刷新数据库列表,以便看到该数据库不再存在。

Testing with this code snippet:

使用此代码段进行测试:

using (var dbContext = new dbContext())
{
    dbContext.Database.Delete();
}

#2


1  

After @moguzalp and this (MSDN Database.Delete Function), I came with a solution for my case:

在@moguzalp和这个(MSDN Database.Delete函数)之后,我为我的案例提供了一个解决方案:

using System.Data.Entity;

Database.Delete("connectionStringOrName");

In my case I was trying to Recreate a mssqllocalDb database for test purposes. But whenever I used the same DbContext (or an immediately new one, disposing the first and opening another), it looked like the database was still up when I tried to create it.

在我的情况下,我试图重新创建一个mssqllocalDb数据库用于测试目的。但每当我使用相同的DbContext(或者立即使用新的DbContext,处理第一个并打开另一个)时,当我尝试创建数据库时,它看起来仍然是数据库。

Bad Example: (x)

不好的例子:(x)

public static void RecreateDatabase(string schemaScript)
{   
    using (DbContext context = new DbContext("connectionStringName"))
    {
        context.Database.Delete(); // Delete returns true, but...
        Database database = context.Database;
        database.Create(); // Database already exists!
        database.ExecuteSqlCommand(schemaScript);
    }
}

Working example: (/)

工作实例:(/)

public static void RecreateDatabase(string schemaScript)
{   
    Database.Delete(); // Opens and disposes its own connection

    using (DbContext context = new DbContext("connectionStringName")) // New connection
    {
        Database database = context.Database;
        database.Create(); // Works!
        database.ExecuteSqlCommand(schemaScript);
    }
}

Context: I'm using this on an [AssemblyInitialize] function to test my ModelContexts

上下文:我在[AssemblyInitialize]函数中使用它来测试我的ModelContexts