如何解决DataEntry模型中SaveChange方法的问题 [英] How to solve the problem about SaveChange Method in DataEntry Model
本文介绍了如何解决DataEntry模型中SaveChange方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我找不到解决方案,对此我感到很疯狂.
请我真的需要帮助.
Hello guys,
I couldn''t find the solution and I am getting crazy about it.
Pls I really need help.
The Error : hata
An error occurred while updating the entries. See the inner exception for details.
public void Ky_DepoInbound()
{
int id = 0;
try
{
MEKMAR_DBEntities mekmar = new MEKMAR_DBEntities();
Mekmar_Server_Data.DataDataContext server = new Mekmar_Server_Data.DataDataContext();
foreach (var st in mekmar.DEPO_INBOUNT)
{
DEPO_INBOUNT inb = (from m in mekmar.DEPO_INBOUNT where m.DP_IN_ID == st.DP_IN_ID select m).Single();
//tablonun silinmesi
mekmar.DeleteObject(inb);
}
mekmar.SaveChanges();
foreach (var st in server.DEPO_INBOUNTs.ToList())
{
DEPO_INBOUNT dpinb = new DEPO_INBOUNT();
dpinb.DP_IN_ID =st.DP_IN_ID;
id = dpinb.DP_IN_ID;
if (st.URUN_ID.HasValue) dpinb.URUN_ID = st.URUN_ID.Value;
else dpinb.URUN_ID = st.URUN_ID;
if (st.BIRIM_FIYAT.HasValue) dpinb.BIRIM_FIYAT = st.BIRIM_FIYAT.Value;
else dpinb.BIRIM_FIYAT = 0;
if (st.KUTU_ADET.HasValue) dpinb.KUTU_ADET = st.KUTU_ADET.Value;
else dpinb.KUTU_ADET = 0;
dpinb.DP_GN_ID = st.DP_GN_ID;
if (st.KUTU_SQF.HasValue) dpinb.KUTU_SQF = st.KUTU_SQF.Value;
else dpinb.KUTU_SQF = 0;
if (st.RF_ID.HasValue) dpinb.RF_ID = st.RF_ID.Value;
else dpinb.RF_ID = 0;
if (st.MUSTERI_ID.HasValue) dpinb.MUSTERI_ID = st.MUSTERI_ID.Value;
else dpinb.MUSTERI_ID = 0;
if (st.DP_BL_ID.HasValue) dpinb.DP_BL_ID = st.DP_BL_ID.Value;
else dpinb.DP_BL_ID = 0;
if (st.KASA_ADET.HasValue) dpinb.KASA_ADET = st.KASA_ADET.Value;
else dpinb.KASA_ADET = 0;
if (st.DEVIR_ID.HasValue) dpinb.DEVIR = st.DEVIR.Value;
else dpinb.DEVIR_ID = 0;
if (st.DEVIR_ID.HasValue) dpinb.DEVIR_ID = st.DEVIR_ID.Value;
else dpinb.DEVIR_ID = 0;
//tablonun eklenmesi
mekmar.AddToDEPO_INBOUNT(dpinb);
}
mekmar.SaveChanges();
}
catch (Exception hata)
{ System.Windows.Forms.MessageBox.Show(hata.ToString()+"\n"+id.ToString());
}
}
等待解决方案..
问候
Haluk
Awaiting solutions..
Regards
Haluk
推荐答案
如果我正确理解了您的代码...
在第二个foreach中,您正在创建每个元素的副本,并将该副本添加到您的实体集中.问题是您还在复制必须是主键的东西(我想是Id).保存更改时,这是冲突的……您的集合中不能有2个具有相同键的对象.
If I understand your code correctly...
in the 2nd foreach you are creating a copy of each element and add that copy to your entities set. The problem is that you are also duplicating what must be your primary key (Id i suppose). When you save changes this is conflictual... You can''t have 2 objects with the same key in your set.
这篇关于如何解决DataEntry模型中SaveChange方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文