我試圖插入使用下面的LINQ查詢。不知何故,我得到了ID的重複記錄。有人能幫我解釋細節嗎?插入重複記錄LINQ
try {
OLA_ASSESSMENT_DTL OAD = db.OLA_ASSESSMENT_DTL.SingleOrDefault(
ev => ev.OAD_RIV_ID == id);
transaction = "Modify"; // resetting the value of the variable
if (OAD == null) {
transaction = "Add";
OAD = new OLA_ASSESSMENT_DTL();
}
OAD.OAD_PER_RELAIONSHIP = relationshipfocus.relationshipfocus;
OAD.OAD_PER_RELAIONSHIP_COMMENT = CRecHelper.CheckIsNullString(relationshipfocus.strrelationshipfocus).ToString();
OAD.OAD_RIV_ID = id;
OAD.OAD_MODIFIED_DATE = DateTime.Now;
if (transaction == "Add") {
db.OLA_ASSESSMENT_DTL.Add(OAD);
}
db.SaveChanges();
}
無論是一個新的對象或沒有,你總是分配ID:'OAD.OAD_RIV_ID = ID;'。你如何期待**不**得到重複? –
您是插入新記錄還是嘗試通過ID更新現有記錄? –
@NaveenKN K N - 我試圖通過ID更新現有的記錄,如果有一個記錄目前的ID。如果沒有記錄,同時插入到具有該ID的同一個表中 – Alai