Debug Lazy loading

Asked

Viewed 55 times

1

Sirs,

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

In the Training controller this way:

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());
    }

The view is 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 see only the query related to the Training, I can’t see the query related to the department, as I see?

in debug:

{SELECT 
    [Extent1].[TreinamentoID] AS [TreinamentoID], 
    [Extent1].[DepartamentoID] AS [DepartamentoID], 
    [Extent1].[NomeTreinamento] AS [NomeTreinamento], 
    [Extent1].[Creditos] AS [Creditos]
    FROM [dbo].[Treinamento] AS [Extent1]}
  • Tower, next time, give a formatted in your code. =)

  • Rubico, ok. It was bad.rs

1 answer

1

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

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

So when one Departamento is at last loaded?

The moment you call him, but specifically in this part:

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

At this time the Entity Framework does a search in 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'

Prior to this moment, he only makes a consultation of Treinamentos normal, that way:

 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'

The queries were generated by Entity Framework on the basic Details screen generated by Scaffolding from Visual Studio 2013.

  • Randrade, I understand your explanation. Very good. However, when can you see this DEPARTMENT query when debugging? I can see only the TRAINING in the controller, running the debug step by step.

  • @Torres This query is seen in SQL Server Profiller a tool from Microsoft.

  • Another thing, assuming that in TRAINING you will have the values "basic C# ", "intermediate C# ", "advanced C# ", "basic Java". And the department has "Dep A", "Dep B", "Dep C", "Dep D" each linked to a specific training. After carrying out the consultation in TRAINING he will carry out the consultation in DEPARTMENT, when this is loaded, then will be carried out 4 consultations in DEPARTMENT to seek their values?

  • @Towers It depends on the way you are doing it. Whether you are using the Lazy Loading, for each different department it will do a search. However, you can use the Include(t => t.Departamento) in his consultation, that he will make a Inner Join on the table Departamentos, which avoids going to the bank so many times.

  • Thank you Randrade, thank you.

  • Indicates a good C# book or course?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.