开发者

c# ConnectionContext and EF will not break DB connection

I'm creating a program which transfers a database from one SQL server (on one PC) to a local instance. After transferring, an API (Entity framework) is going to connect to it and run some things on it. It will then disconnect, and two SQL scripts will be ran on it via the SqlCommand class. After that, the database will be backed up and saved and the local, temporary database will be dropped. The problem I'm running into is that the SqlCommand instance that first connects (before the transfer takes place) which creates the database, disconnects without an issue (I check the active connections via management studio). But once the entities context connects, even with a context.Dispose() call, it doesn't disconnect. Then I create an Server instance to run SQL scripts (located in a sub folder) with code similar to this:

FileIn开发者_JAVA技巧fo file = new FileInfo(filename);
string removeRecords = file.OpenText().ReadToEnd();
file.OpenText().Close();

Server srv = new Server(new ServerConnection(this.myScriptConn));
ServerConnection srvConn = new ServerConnection();
srvConn.NonPooledConnection = true;
srvConn = srv.ConnectionContext;

Server server = new Server(srvConn);

server.ConnectionContext.ExecuteNonQuery(removeRecords);

server.ConnectionContext.Disconnect();

The Disconnect call here doesnt break the connection either. So when I go to drop the database, I find that I can't because there are 2 active connections. Is there something that I'm doing wrong here or something that I'm missing? The NonPooledConnection = true was something that I found online to try if the Disconnect didn't work but obviously that didnt either. Otherwise my code would look like:

FileInfo file = new FileInfo(filename);
string removeRecords = file.OpenText().ReadToEnd();
file.OpenText().Close();

Server server = new Server(new ServerConnection(this.myScriptConn));
server.ConnectionContext.ExecuteNonQuery(removeRecords);

server.ConnectionContext.Disconnect();

I am sure that I am disconnecting prior connections before making any new ones. The only way to disconnect the entities is to close the program and this is not what I want obviously.

Any help would be appreciated, thanks.


I just found out that doing a SqlConnection.ClearAllPools() fixes the issue and disconnects all active connections.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜