问题
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