When you get into DB level issues with your .NET framework application that is using Entity framework, you need to start to examine what is happening under the covers using tools like SQL profiler and/or Entity framework profiler. These can help you track issues and work out strategies to minimise or eliminate nasties like deadlocks.
One tool that worked well for me is the entity framework profiler. To utilise this, you need to initialise this with your application code then run the diagnostic tool to be able to capture the requests going into SQL.
I did have to do a bit of fiddling around as I could not get it to work out of the box with my current setup of an OWIN hosted project. I noticed the entity framework was being instantiated before the profiler and as a result threw the following exception at startup
The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
I had the following line of code in the Startup method like so which did NOT work
[assembly: OwinStartup(typeof(Startup))] namespace MyApp { public partial class Startup { public void Configuration(IAppBuilder app) { HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize(); // other code
}
To get around this, I had to change it as follows
[assembly: OwinStartup(typeof(Startup))]
// added line
[assembly: PreApplicationStartMethod(typeof(Startup), "Register")]
namespace MyApp
{
public partial class Startup
{
// added method
public static void Register()
{
HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();
}
public void Configuration(IAppBuilder app)
{
// other code here
}
Comments
Post a Comment