linq to sql 中添加在数据库外部已经删除的数据异常
执行一次后,在外部把记录手工删除,再次执行时报错,{System.Data.Linq.DuplicateKeyException: 不能添加其键已在使用中的实体。
在 System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)
在 System.Data.Linq.ChangeProcessor.TrackUntrackedObjects()
在 System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
在 System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
在 LinqUpdateView.Form1.button5_Click(Object sender, EventArgs e) 位置 H:\快盘\testLinqUpdateView\LinqUpdateView\LinqUpdateView\Form1.cs:行号 33}
源码 :
private void button5_Click(object sender, EventArgs e)
{
List<ScoreMapping> tmpL = getsm();
try
{
//linq = new DataClasses1DataContext();
linq.ScoreMapping.InsertAllOnSubmit(tmpL);
//linq.ScoreMapping.InsertOnSubmit(tmpL[0]);
linq.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
MessageBox.Show("success");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
foreach (ObjectChangeConflict cc in linq.ChangeConflicts)
{
if (cc.IsDeleted)
{
linq.ScoreMapping.InsertAllOnSubmit(tmpL);
}
}
linq.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
linq.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
MessageBox.Show("success");
}
}
private List<ScoreMapping> getsm()
{
List<ScoreMapping> tmpL = new List<ScoreMapping>();
for (int i = 0; i < 3; i++)
{
ScoreMapping sm = new ScoreMapping();
sm.branchId = "test";
sm.mICcId = "mICcId";
sm.quantity = 10;
sm.scoreId = "scoreId" + i;
sm.updateStatus = '0';
sm.ValidDate = DateTime.Now;
tmpL.Add(sm);
}
return tmpL;
} linq sql --------------------编程问答-------------------- 添加 这个后可以解决 //linq = new DataClasses1DataContext();
但这样每次重新创建新上下文会不会耗内存及性能 --------------------编程问答-------------------- 重启工作进程所有的应用程序池,修改web.config也可以,重新生成bin下的dll也行, --------------------编程问答-------------------- you can change your code like this:
try
{
}
catch
{
}
finally
{
linq.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
}
补充:.NET技术 , LINQ