Excessive SQL Server processes for Entity Framework based application
We are currently developing a WPF/Entity-Framework based application. One of our requirements was to allow the user option to select which environment (testing, training, or production) they will connect to when logging in. Each connection option points to its own database. In order to do this we are creating new EF contexts in the following way:
Public Function NewVectorContext() As VectorEntities
Dim strDBConn As String = "metadata=res://*/EntityFramework.VectorModel.csdl|res://*/EntityFramework.VectorModel.ssdl|res://*/EntityFramework.VectorModel.msl;provider=System.Data.SqlClient;provider connection string=""" & VectorConnectionString & """"
Dim EntityConn As System.Data.EntityClient.EntityConnection = New System.Data.EntityClient.EntityConnection(strDBConn)
Dim MyNewContext as VectorEntities = New VectorEntities(EntityConn)
MyNewContext.ContextOptions.LazyLoadingEnabled = False
Return MyNewContext
End Function
VectorConnectionString is formatted in standard SQL Server fashion:
Data Source=OurDBServer;Initial Catalog=OurAppDB;Persist Security Info=True;User ID=OurUserId;Password=OurPassword
and is determined at runtime by user's selection (testing, training, prod).
We have made sure that all new contexts are being disposed of once its job is completed.
The problem is this: when the users are using the application we are seeing hundreds of SQL Server processes 开发者_如何学运维in SQL Server's activity monitor. Some individual users have upwards of 30+ processes associated when using the app. Once the user shuts down the app the associated processes all disappear - but while the app is running these processes seem to stick around.
Fixed - as I mentioned above part of the solution was proper memory management and making sure that all models and viewmodels were being properly disposed of. But that only eliminated a small number of the sql server processes we were seeing. The major culprit was an improperly designed model base class that was creating multiple contexts for each model being instantiated. Once it was rewritten the problem was solved. Nothing to do with EF.
精彩评论