Problems with slowness SQL LINQ C # Entity FrameWork

2

I'm using Entity frameWork, I have a list of objects (data taken from the database) with Client data,

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


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

Then in my form, I have a Grid

grid.datasource = clientes.ToList();

I'm calling the list on the 'On Load' form, I'm only 3 records in the database (SQLSERVER), and it takes more than 10 seconds to open the form.

In the class mapped the PK looks like this:

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

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

    
asked by anonymous 06.11.2015 / 22:13

2 answers

4

By its answer , the problem of slowness comes from the lazy load, which possibly is made for a large result . It's going to slow down.

The function below resembles a repository, which is a very wrong approach to using with Entity Framework, which already implements a repository. However, loading anticipation (or simply using 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 even used, but I just kept it as an example.

    
23.11.2015 / 04:03
0

I was using the context to do all the searches, so when I gave the command db.clientes.ToList (), inside my context class I have other templates, so it passed one by one (creating the mapping), so I create a query for each event, so I do not let the entity mount the select for me, I 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>;
    }

Moving to the GridView:

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

It helped a lot in my case, however I'm working on this project with a friend, using the same type of notbook and same version of visual stuido (Community 2015) and when he opened the form, it would not be long.

I hope I have helped!

    
22.11.2015 / 22:23