سؤال

I have a MVC 4 Web Application that uses a BLL and DAL. The DAL uses EF6 and the model first approach. I would like to setup MiniProfiler to profile the web and database calls. I've added MiniProfiler and MiniProfiler.MVC4 through Nuget and it is up and running on the website.

My question is, how can I setup the BLL and DAL to return EF calls with query information?

Here is how the projects are setup:

Web Layer - Reference to MiniProfiler, MiniProfiler.MVC, and BLL Project. The Controllers make the call to the BLL methods.

BLL - Reference to MiniProfiler and the DAL Project. BLL methods make a call to the DAL methods.

DAL - Reference to MiniProfiler and MiniProfiler,EF5. DAL methods make a call to the database using Linq.

Just based on that setup, I can get MiniProfiler data from the BLL but I'm not getting any EF SQL data.

هل كانت مفيدة؟

المحلول

Ok, I got it figured out. Here is how to setup your N-Tier projects to support MiniProfiler:

Web Layer - Add a reference to MiniProfiler, MiniProfiler.MVC, MiniProfiler.EntityFramework and BLL Project. In the Global.asax, make sure to turn on EF Profiling:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // Entity Framework Profiling
        MiniProfilerEF.Initialize();
    }

Here is an example of a controller calling the BLL with profiling:

    [HttpGet]
    public ActionResult Index()
    {
        var profiler = MiniProfiler.Current;

        using (profiler.Step("Web Controller"))
        {
            Employee bll = new Employee();
            int value = bll.GetLastEmployeeID();
        }

        return View();
    }

BLL - Add a reference to MiniProfiler and the DAL Project. BLL methods make a call to the DAL methods.

Here is an example of the a BLL method calling the DAL with profiling:

    public int GetLastEmployeeID()
    {
        int result = 0;
        var profiler = MiniProfiler.Current;

        using (profiler.Step("BLL - GetLastEmployeeID"))
        {
            EmployeeDAO dao = new EmployeeDAO();

            result = dao.GetLastEmployeeID();
        }

        return result;
    }

DAL - Add a reference to MiniProfiler and MiniProfiler,EF5. DAL methods make a call to the database using Linq. For example:

    public int GetLastEmployeeID()
    {
        int id = 0;

        using (var context = new CompanyEntities())
        {
            var lastEmployee = (from e in context.Employees
                               where e.IsDeleted == false
                               orderby e.EmployeeID descending
                               select e).First();

            id = lastEmployee.EmployeeID;
        }

        return id;
    }

Using this setup, I am able to get the EF profiling with SQL to display in MiniProfiler on the Web Site.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top