为什么要使用实体框架获取数据缓慢? [英] Why getting data with Entity Framwork is slow?
本文介绍了为什么要使用实体框架获取数据缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一切都很好,但性能太差,即使数据不足也是太慢的速度。我的代码在下面,我想知道我的代码有什么问题。
我搜索了很多,但没有任何帮助。
提前感谢
私有readonly BarForooEntities1 _barforoosh = new BarForooEntities1();
public void Getdataforoosh()
{
BindingSource b = new BindingSource();
b.DataSource =(从m在_barforoosh.RadifsSendCenter
其中m.Receive == false
选择新
{
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 ]。值);
var f =(来自_barforoosh.RadifsSendCenter
其中a.id_rec == c
选择a).SingleOrDefault();
f.Receive = true;
_barforoosh.SaveChanges();
}
}
解决方案
SaveChanges
方法
循环之外
$ b (int i = 0; i< dataGridView1.RowCount; i ++)
{
}
_barforoosh.SaveChanges(); $ b
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();
}
}
解决方案
Put SaveChanges
method outside of for
loop:
for (int i = 0; i < dataGridView1.RowCount; i++)
{
}
_barforoosh.SaveChanges();
这篇关于为什么要使用实体框架获取数据缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文