Why getting data with Entity Framwork is slow? [closed]

偶尔善良 提交于 2019-12-13 09:29:43

问题


I am creating a Windows forms program with C# and Entity Framework. I want to fetch data from SQL database and do some processing after fetching it.

Everything is good but the performance is too bad with too slow speed even with little data. My code is below and I want to know what is wrong with my code.

I searched a lot but nothing found helpful.

Thanks in advance

private readonly BarForooEntities1 _barforoosh = new BarForooEntities1();

public void Getdataforoosh()
{
    BindingSource b = new BindingSource();

    b.DataSource = (from m in _barforoosh.RadifsSendCenter
                    where m.Receive == false
                    select new
                           {
                               m.id_rec, m.Radifkolsal,
                               m.Dates, m.DateErsal,
                               m.TimeErsal, m.Karkhane,
                               m.Namekala, m.Vazn,
                               m.Bandal, m.Dobaskul,
                               m.OldYear, m.Sal,
                               m.del, m.edit, m.Daryaft,
                               m.Shobe, m.Greid, m.TedadBas, 
                               m.Rahgiry, m.Tozih, m.NoeShemsh, 
                               m.Metrazh, m.Keyfiat, m.Address,
                               m.City, m.Karbar, m.CodeKala,
                               m.CodeGoruh, m.CodeKG, m.CodeGreid,
                               m.Tel, m.ShenaseMeli, m.Sefaresh,
                               m.Tolid, m.Shenase, }).ToList();
    dataGridView1.DataSource = b;

    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
        int c = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);

        var f = (from a in _barforoosh.RadifsSendCenter
                 where a.id_rec == c
                 select a).SingleOrDefault();
        f.Receive= true;
        _barforoosh.SaveChanges();
    }
}

回答1:


Put SaveChanges method outside of for loop:

for (int i = 0; i < dataGridView1.RowCount; i++)
{
}
_barforoosh.SaveChanges();


来源:https://stackoverflow.com/questions/37096509/why-getting-data-with-entity-framwork-is-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!