Skip to main content

Running Entityframework profiler on OWIN hosted .NET Framework projects

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

Popular posts from this blog

3 Benefits of getting into grassroots charities and volunteering

Volunteering at grass roots levels gives you a great satisfaction in that you know your small efforts has reached and helped someone in need. Whether it be, standing behind a counter selling some quick bites or just spending a few minutes with someone in need of company, there are so many life long traits and attributes that you pick up that you may not even realise.  The skills you pick up not only help your causes, but they also go a long way in your professional journey.  Here are 3 of the most important benefits I have come to realise about getting into grass roots charities and doing good for the people early on. 1.  Social skills Without doubt, it is one of the first skills you pick up in volunteering that goes a long way. Talking and engaging with people makes it so much easier to be able to progress in your career. It opens up new opportunities and gets you prepared for various situations. More likely than not, as a volunteer, you get the opportuni...

Hello and Welcome!

Welcome to Srini's blog! This is my first blog post. Hopefully the following will give you some information on what my blogs will be about class Person { string FirstName = "Srini"; string LastName = "Vasudevan"; string Occupation = "INSERT SOME SOFTWARE ROLE HERE"; List<string> Hobbies = new List { "software development", "cricket", "community and charity work", "technology" }; } If you want to know more about my professional background, you can check out my LinkedIn profile at http://au.linkedin.com/in/sriniv85 If you want to know about the organisation I help in my spare time by doing some charitable work, please visit  http://www.sriomcare.org.au

Saleforce CRM for Sri Om Foundation – success story

Working with a fast growing not-for-profit organisation, I have come to realise the importance of having some systems in place for everyday activities. When your e-mail starts to grow, endless message chains, and you lose focus on the tasks ahead of you, that is the time your mind starts to think… Surely there must be a better way to do this? This is the time when powerful IT systems come to play. I have been lucky and privileged enough to work with Salesforce (http://salesforce.com) for Sri Om Foundation. As an executive member of Sri Om, and having a good understanding of our processes, I have had the opportunity to put in place certain systems to streamline our work flow. Off course, having a software background and a passion for these kinds of tools does always help! It has probably taken a bit of time to adopt, and as an organisation, we are still facing challenges, but the platform has been set for us to only develop and making it easier for us to tackle these issues head...