仅插入新数据而不是复制现有数据

时间:2014-06-05 08:00:47

标签: c# asp.net linq entity-framework-4

我正在运行此代码,从核心系统调用数据

  public int MuatTurunMTS(hopesWcfRef.Manifest2 entParam, string destination, int capacity)
        {
        try
        {
            EL.iSeriesWcf.IiSeriesClient iClient = new iSeriesWcf.IiSeriesClient();

            EL.iSeriesWcf.Manifest2 manifest2 = new iSeriesWcf.Manifest2();
            manifest2 = iClient.GetManifest2(entParam.NOKT, destination, capacity);

            DataTable dt = new DataTable();
            dt = manifest2.Manifest2Table;

            List<hopesWcfRef.Manifest2> LstManifest2 = new List<hopesWcfRef.Manifest2>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                hopesWcfRef.Manifest2 newDataRow = new hopesWcfRef.Manifest2();

                newDataRow.NOPASPOT = dt.Rows[i][1].ToString();
                newDataRow.NOKT = dt.Rows[i][0].ToString();
                newDataRow.KOD_PGKT = dt.Rows[i][2].ToString();
                newDataRow.KOD_KLGA = dt.Rows[i][3].ToString();
                newDataRow.NAMA = dt.Rows[i][4].ToString();
                newDataRow.TKHLAHIR = (dt.Rows[i][5].ToString() == "1/1/0001 12:00:00 AM" ? DateTime.Now : Convert.ToDateTime(dt.Rows[i][5]));  //Convert.ToDateTime(dt.Rows[i][5]);
                newDataRow.JANTINA = dt.Rows[i][6].ToString();
                newDataRow.WARGANEGARA = dt.Rows[i][7].ToString();
                newDataRow.JNS_JEMAAH = dt.Rows[i][8].ToString();
                newDataRow.NO_SIRI = dt.Rows[i][9].ToString();
                newDataRow.NO_MS = Convert.ToInt16(dt.Rows[i][10]);
                newDataRow.BARKOD = dt.Rows[i][13].ToString();
                newDataRow.NO_DAFTAR = dt.Rows[i][14].ToString(); 

                //bydefault make cell empty
                newDataRow.STS_JEMAAH = "";
                newDataRow.SEAT_NO = "";
                newDataRow.SEAT_ZONE = "";
                newDataRow.BERAT = 0;
                newDataRow.JUM_BEG = 0;

                LstManifest2.Add(newDataRow);
            }

            int cntr = 0;
            if (LstManifest2.Count != 0)
            {
                foreach (hopesWcfRef.Manifest2 manifest in LstManifest2)
                {
                    cntr++;
                    SaveManifestTS(manifest);
                }
            }

            return LstManifest2.Count;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

转到:

 public void SaveManifestTS(hopesWcfRef.Manifest2 manifest)
         {
             try
             {
                 ent.BeginSaveChanges(SaveChangesOptions.Batch, null, null);
                 ent.AddObject("Manifest2", manifest);
                 ent.SaveChanges(SaveChangesOptions.Batch);
              }
             catch (Exception ex)
             {
                 //Uri u = new Uri(ent.BaseUri + "GetErrorMsg"
                 //              , UriKind.RelativeOrAbsolute);
                 //string datas = (ent.Execute<string>(u)).FirstOrDefault();
                 //Exception ex = new Exception(datas);
                 throw ex;
             }
         }

当SaveChanges运行时,如果数据存在,它将复制整行, 插入时如何避免重复数据(savechanges)?????? 非常感谢

1 个答案:

答案 0 :(得分:0)

怎么样:不要插入。

在这些情况下,我使用MERGE语句更新现有数据(基于主键)并插入新数据。

哦,你喜欢发布的所有代码示例与问题完全无关,这是一个纯粹的SQL问题。你真的引用你的汽车手册,然后询问转向哪个方向。