0

我的问题是,如果我遍历我的集合,如下所示,并适当地设置值,我是否需要dc.SubmitChanges()在每次迭代结束时调用它才能正常工作?如果我不这样做,它不会简单地覆盖更新上的先前值吗?

我将一个Contact对象传递给这个Save()方法。该对象具有PersonContact属性,而该属性又具有Phones属性。这是Save()方法的一部分,需要Phones在我的对象中循环并与它们进行比较DB并采取适当的措施。

有没有更好、更简单的方法?

If contact.PersonContact.Phones IsNot Nothing Then
  For Each phone In contact.PersonContact.Phones
    Dim exists = dc.tblPhones.SingleOrDefault(Function(p) p.ID = phone.ID)
    If exists IsNot Nothing Then
        If phone.Number = String.Empty AndAlso phone.Extension = String.Empty Then
            'Delete
            dc.tblPhones.DeleteOnSubmit(exists)
        Else
            'Update
            exists.ID = phone.ID
            exists.PhoneNumber = phone.Number
            exists.TypeID = phone.TypeID
            exists.UpdateDate = DateTime.Now
            exists.UpdateUser = user
        End If
    Else
        'Insert
        exists = New tblPhone() With { _
            .ID = phone.ID, _
            .PhoneNumber = phone.Number, _
            .Extension = phone.Extension, _
            .TypeID = phone.TypeID, _
            .InsertDate = DateTime.Now, _
            .InsertUser = user, _
            .PersonID = contact.PersonContact.ID _
        }
        dc.tblPhones.InsertOnSubmit(exists)
    End If
  'Do I need to Save here???
  dc.SubmitChanges()
  Next
End If
4

0 回答 0