在我的项目中,我有两个独立的服务器,具有单独的实体上下文,第一个我从一个服务器获取数据并通过此代码将其保存到列表中:
BarForooEntities1 bfe = new BarForooEntities1();
SaleHistory sh = new SaleHistory();
RadifsSendCenter rs = new RadifsSendCenter();
BaskoolEntities be = new BaskoolEntities();
var q = (from d in bfe.RadifsSendCenter
where d.Daryaft == true
orderby d.id_rec
select d).ToList();
然后我使用以下代码将该列表保存到另一个服务器数据库:
foreach (var v in q)
{
sh.id_rec = v.id_rec;
sh.Radifkolsal = v.Radifkolsal;
sh.Dates = v.Dates;
sh.DateErsal = v.DateErsal;
sh.TimeErsal = v.TimeErsal;
sh.Karkhane = v.Karkhane;
sh.Namekala = v.Namekala;
sh.Vazn = v.Vazn;
sh.Bandal = v.Bandal;
sh.Dobaskul = v.Dobaskul;
sh.OldYear = v.OldYear;
sh.Sal = v.Sal;
sh.del = v.del;
sh.edit = v.edit;
sh.Daryaft = v.Daryaft;
sh.Shobe = v.Shobe;
sh.Greid = v.Greid;
sh.TedadBas = v.TedadBas;
sh.Rahgiry = v.Rahgiry;
sh.Tozih = v.Tozih;
sh.NoeShemsh = v.NoeShemsh;
sh.Metrazh = v.Metrazh;
sh.Keyfiat = v.Keyfiat;
sh.Address = v.Address;
sh.City = v.City;
sh.Karbar = v.Karbar;
sh.CodeKala = v.CodeKala;
sh.CodeGoruh = v.CodeGoruh;
sh.CodeKG = v.CodeKG;
sh.CodeGreid = v.CodeGreid;
sh.Tel = v.Tel;
sh.ShenaseMeli = v.ShenaseMeli;
sh.Sefaresh = v.Sefaresh;
sh.Tolid = v.Tolid;
sh.Shenase = v.Shenase;
var u = (from bu in be.BarbariUsers
where bu.UserName == Login.username
select bu).FirstOrDefault();
sh.UserID = u.UserID;
sh.ReceiveUser = Login.username;
sh.ReceiveDateTime = DateTime.Now;
be.SaleHistory.Add(sh);
rs.Daryaft = false;
}
be.SaveChanges();
bfe.SaveChanges();
MessageBox.Show("Done!");
当我运行我的项目时,一切正常,但只有一行列表被添加到第二个列表中,并且更新第一个数据库值(rs.Daryaft = false;)也没有任何改变。 我知道如果我在foreach循环中移动(be.SaveChanges();)和(bfe.SaveChanges();),那么第一个工作需要很长时间而第二个工作不能做任何事情。也许我走错了路。 有任何建议或其他解决方案来解决这个问题吗?
答案 0 :(得分:0)
Finlay我解决了自己的问题。这是编辑过的代码,可能对某人有用:
BaskoolEntities be = new BaskoolEntities();
BarForooEntities1 bfe = new BarForooEntities1();
var q = (from d in bfe.RadifsSendCenter
where d.id_rec > last
orderby d.id_rec
select new
{d.id_rec,d.Radifkolsal,d.Namekala,d.Vazn,d.Bandal,d.Greid,d.TedadBas,d.Tozih,d.NoeShemsh,d.Metrazh,d.Keyfiat,d.Address,d.City,d.Tel,d.ShenaseMeli,d.Sefaresh,d.Tolid,d.Shenase
}).ToList();
{
foreach (var v in q)
{
RadifsSendCenter s = new RadifsSendCenter();
SaleHistory sh = new SaleHistory();
sh.id_rec = v.id_rec;
sh.Radifkolsal = v.Radifkolsal;
sh.Namekala = v.Namekala;
sh.Vazn = v.Vazn;
sh.Bandal = v.Bandal;
sh.Greid = v.Greid;
sh.TedadBas = v.TedadBas;
sh.Tozih = v.Tozih;
sh.NoeShemsh = v.NoeShemsh;
sh.Metrazh = v.Metrazh;
sh.Keyfiat = v.Keyfiat;
sh.Address = v.Address;
sh.City = v.City;
sh.Tel = v.Tel;
sh.ShenaseMeli = v.ShenaseMeli;
sh.Sefaresh = v.Sefaresh;
sh.Tolid = v.Tolid;
sh.Shenase = v.Shenase;
var u = (from bu in be.BarbariUsers
where bu.UserName == Login.username
select bu).FirstOrDefault();
sh.UserID = u.UserID;
sh.ReceiveUser = Login.username;
sh.ReceiveDateTime = DateTime.Now;
var b= (from h in bfe.RadifsSendCenter
where h.id_rec == q.id_rec
select h).FirstOrDefault();
b.Daryaft = false;
be.SaleHistory.Add(sh);
}
be.SaveChanges();
bfe.SaveChanges();
}
MessageBox.Show("Done!");
}