Slow SQL LINQ C# Entity Framework problems

Asked

Viewed 800 times

2

I am using Entity Framework, I have a list of objects (data taken from the database) with data from Clients,

public virtual DbSet<clientes> clientes {get;set;}


public IEnumerable<clientes> FindAll()
{   db.Configuration.LazyLoadingEnabled = false;
    return db.clientes.ToList();
}

and then on my form, I have a Grid

grid.datasource = clientes.ToList();

I’m calling the list right in the 'On Load' form, I’m only with 3 record in the database (SQLSERVER) and it’s taking more than 10 seconds to open the form..

In the class mapped the PK is this way :

[Key]
public int pk_cliente {get;set;}

I was wondering if the PK mapping is wrong and it’s getting lost somewhere.

  • 2

    Have you tried profiling your system before? I don’t think Entity Framework is the reason for the slowness, so I said.

  • Instead of having a Direct Reset on . Tolist() you create a variable and throw the tolist into it, at the moment of tolist it goes on the bench and back. The time to run this line will be the time spent with the database. Try this and speak the result.

  • Managed to solve?

  • @Renan yes, I got it! I posted the answer, thanks for asking.

2 answers

4


For his reply, the slowness problem comes from the lazy load, which is possibly made for a great result. It will get slow even.

The function below resembles a repository, which is a very wrong approach to use with Entity Framework, which already implements a repository. Either way, load anticipation (or simply, use JOIN for relational databases) can be done as follows:

public IEnumerable<modelos> FindBy(string valor)
{
       return db.modelos
                 .Include(m => m.Peca)
                 .Include(m => m.Produto)
                 .AsEnumerable();
}

valor is not used or used, but I have kept it as an example.

0

I was using the context to do all the research, so when I gave the command db.clientes.Tolist(), within my context class I have other models, so he would go one by one (creating the mapping), then I would create a query for each event, so don’t let Entity mount select for me, I’ll do it myself as follows :

    private Context db = new Context();
    private IQueryable query;   


 public IEnumerable<modelos> FindBy(string valor)
    {
           query = from m in db.modelos
                   join p in db.pecas on m.fk_Peca equals p.pk_Peca
                   join po in db.produtos on m.fk_Produto equals  po.pk_Produto
                   select new { m,p,po};

        return query as IEnumerable<modelos>;
    }

Switching to the Gridview :

gridViewDetalhes.DataSource = _Modelos.FindBy(edtCodigoModelo.Text).ToList();

It helped a lot in my case, however I am working on this project with a friend, using the same type of notbook and same version of visual stuido (Community 2015) and when he was opening the form, it didn’t take long.

I hope I’ve helped!

  • 1

    Not quite this the problem. You solved it by anticipating the load. I will write an answer that gives a slightly more current approach to your problem.

Browser other questions tagged

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