Debugar lazy loading

1

Sirs,

I would like to view all the queries that are generated when I use lazy loading. When debugging only the main view.

In the Training controller it looks like this:

public class TreinamentosController : Controller
{
    private DbContext db = new DbContext();
    public ActionResult Index()
    {
        db.Configuration.LazyLoadingEnabled = true;
        IList<Treinamento> treinamentos = b.Treinamentos.ToList<Treinamento>();
        //var treinamentos = db.Treinamentos.Include(t =>t.Departamentos);
        return View(treinamentos.ToList());
    }

In the view like this:

<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.Departamentos.NomeDepartamento)</th>
        <th>@Html.DisplayNameFor(model => model.NomeTreinamento)</th>
        <th>@Html.DisplayNameFor(model => model.Creditos)</th>
        <th></th>
    </tr>

    @foreach(var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.Departamentos.NomeDepartamento)</td>
            <td>@Html.DisplayFor(modelItem => item.NomeTreinamento)</td>
            <td>@Html.DisplayFor(modelItem => item.Creditos)</td>
            <td>
                @Html.ActionLink("Editar", "Editar", new { id = item.TreinamentoID}) |
                @Html.ActionLink("Detalhes", "Detalhes", new { id = item.TreinamentoID }) |
                @Html.ActionLink("Excluir", "Excluir", new { id = item.TreinamentoID }) 
            </td>

        </tr>
    }
</table>

When debugging I only see the query related to Training, I can not see the query for the department, how do I visualize it?

no debug:

{SELECT 
    [Extent1].[TreinamentoID] AS [TreinamentoID], 
    [Extent1].[DepartamentoID] AS [DepartamentoID], 
    [Extent1].[NomeTreinamento] AS [NomeTreinamento], 
    [Extent1].[Creditos] AS [Creditos]
    FROM [dbo].[Treinamento] AS [Extent1]}
    
asked by anonymous 16.06.2016 / 16:38

1 answer

1

This answer explains very well what is Lazy Loading and how it works in Entity Framework , but in summary:

  

In this case, Departamento is not loaded when a Training is loaded. When searching for the Training data, what is completed in Department are classes of type DynamicProxy. These entities will only become objects of their respective Models when they are accessed directly.   - @CiganoMorrisonMendez

So when does a Departamento end load?

At the moment you call, but specifically in this part:

 <td>@Html.DisplayFor(modelItem => item.Departamentos.NomeDepartamento)</td>

At this point the Entity Framework does a search on your database and brings back the expected Department. The query he does is this (I just summarized the fields):

exec sp_executesql N'SELECT 
    [Extent1].[DepartamentoId] AS [DepartamentoId], 
    [Extent1].[Nome] AS [Nome]
    FROM [dbo].[Departamentoes] AS [Extent1]
    WHERE [Extent1].[DepartamentoId] = @EntityKeyValue1',N'@EntityKeyValue1 uniqueidentifier',@EntityKeyValue1='A96D7FCE-6FFE-4F97-B6DD-9A83DD8B0109'

Before this moment, it only queries for Treinamentos normal, like this:

 exec sp_executesql N'SELECT TOP (2) 
    [Extent1].[TreinamentoId] AS [TreinamentoId], 
    [Extent1].[Nome] AS [Nome], 
    [Extent1].[DepartamentoId] AS [DepartamentoId]
    FROM [dbo].[Treinamentoes] AS [Extent1]
    WHERE [Extent1].[TreinamentoId] = @p0',N'@p0 uniqueidentifier',@p0='72A8B3DC-A998-47D8-BCF6-D8CDF21DBD29'
  

Queries were generated by the Entity Framework in the Basic Details screen generated by Visual Studio 2013 Scaffolding.

    
16.06.2016 / 18:12