多次附加/插入

时间:2016-11-03 11:28:13

标签: vb.net linq

我有一个Sub,它需要很多值,并根据需要更新/创建数据库中的记录。

Public Sub BatchChange(ByVal Updated As List(Of Article), ByVal Created As List(Of Article), ByVal Deleted As List(Of Article), ByVal UserId As Integer)

        Dim d As DateTime = Now

        For Each Art As Article In Deleted
            Art.Deleted = True
            Art.ChangeDate = d
            Art.ChangeCount += 1
            DB.Articles.Attach(A, True)
        Next

        For Each Art As Article In Created
            Art.ChangeDate = d
            Art.ChangeCount = 1
            Art.ModifierUserId = UserId
            DB.Articles.InsertOnSubmit(A)

            Next

            For Each Art As Article In Updated
                Art.ChangeDate = d
                Art.ChangeCount += 1
                Art.ModifierUserId = UserId
                DB.Articles.Attach(A, True)

            Next

        DB.SubmitChanges()

End Sub

现在这又必须通过REST-API同步到另一个数据库,而我当前的问题是" Created"模型。

    Public Sub BatchChange(ByVal Updated As List(Of Article), ByVal Created As List(Of Article), ByVal Deleted As List(Of Article), ByVal UserId As Integer)

        Dim d As DateTime = Now
        Dim SyncList As New List(Of SyncParam)

        For Each A As Article In Deleted
            A.Deleted = True
            A.ChangeDate = d
            A.ChangeCount += 1
            DB.Articles.Attach(A, True)
            SyncList.Add(New SyncParam(A.ClientId, A.ArticleId))
        Next

        For Each A As Article In Created
            A.ChangeDate = d
            A.ChangeCount = 1
            A.ModifierUserId = UserId
            DB.Articles.InsertOnSubmit(A)
            SyncList.Add(New SyncParam(A.ClientId, A.ArticleId))
            Next

            For Each A As Article In Updated
                A.ChangeDate = d
                A.ChangeCount += 1
                A.ModifierUserId = UserId
                DB.Articles.Attach(A, True)
            SyncList.Add(New SyncParam(A.ClientId, A.ArticleId))
            Next

        DB.SubmitChanges()

        For Each Value In SyncList
            API.Util.APISync(Value.ClientId, Value.ArticleId)
        Next

    End Sub

我需要为文章创建ID,所以我在DB.SubmitChanges之后循环遍历列表,徒劳地希望检索它,然后将其与其他数据库同步。

但显然这种方式是不可能的,它一直在抱怨该行已经被更改/修改或删除,我假设它已经更新了这些值。

如何在没有VB Net抱怨的情况下循环列表(使用修改后的值)?

1 个答案:

答案 0 :(得分:0)

我发现Created对象在DB.SubmitChanges之后确实包含了ID,所以答案是在那之后创建一个新的For Each循环并拉出ID

Public Sub BatchChange(ByVal Updated As List(Of Article), ByVal Created As List(Of Article), ByVal Deleted As List(Of Article), ByVal UserId As Integer)

    Dim d As DateTime = Now
    Dim SyncList As New List(Of SyncParam)

    For Each A As Article In Deleted
        A.Deleted = True
        A.ChangeDate = d
        A.ChangeCount += 1
        DB.Articles.Attach(A, True)
        SyncList.Add(New SyncParam(A.ClientId, A.ArticleId))
    Next

    For Each A As Article In Created
        A.ChangeDate = d
        A.ChangeCount = 1
        A.ModifierUserId = UserId
        DB.Articles.InsertOnSubmit(A)
        Next

        For Each A As Article In Updated
            A.ChangeDate = d
            A.ChangeCount += 1
            A.ModifierUserId = UserId
            DB.Articles.Attach(A, True)
        SyncList.Add(New SyncParam(A.ClientId, A.ArticleId))
        Next

    DB.SubmitChanges()

    For Each A as Article in Created
       SyncList.Add(New SyncParam(A.ClientId, A.ArticleId))
    Next

    For Each Value In SyncList
        API.Util.APISync(Value.ClientId, Value.ArticleId)
    Next

End Sub