Database First approach with Lazy Loading disabled?

Asked

Viewed 40 times

1

I’m starting a project and the Sql Server already exists so I will take the approach Database First as an example of that article Entity Framework Tutorial.
I’ll create the file .emdx and import the respective tables Tb_client, Tb_addressaco, Tb_product, Tb_stock and n other associated tables .
Suppose I do this:

static void Main(string[] args)
{
    clienteCTX context = new clienteCTX();
    var _model = context.cliente.where(x => x.clienteID).ToList()
}

The above query will return all Entities associated with the Client Entity, correct ?
Doubt: can I disable the Lazy Loading for me to press the expression lambda only data I will really need using the include ? :

If Yes like me disabled the Lazy Loading in this scenario adopting the Database First ?

1 answer

0

The simplest way is to directly disable the Lazy Loading:

static void Main(string[] args)
{
    clienteCTX context = new clienteCTX()
    {
        Configuration.LazyLoadingEnabled = false
    };

    var _model = context.cliente.where(x => x.clienteID).ToList();
}

I believe that this form will also work in Database First (as it works in Code First).

In the Soen I have also found this form, but which I have never experienced:

public static dynamic GetListaClientes()
{
    using (var context = new clienteCTX())
    {
        try
        {
            context.Configuration.ProxyCreationEnabled = false;
            return context.cliente.where(x => x.clienteID).ToList();
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

Link: Entity Framework: How to disable Lazy loading for specific query?

Browser other questions tagged

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