LinqToSQL更新:每个更改属性的一个语句

时间:2013-01-21 11:16:26

标签: vb.net linq-to-sql

当我尝试更新LinqToSQL对象时,引擎会在DataContext.getChangeSet().Updates为我更改的每个属性创建一个条目。 由于覆盖检查,这显然会失败......

如何更正此行为(所有更改都有一次更新)?

正如您在生成的SQL代码中所看到的,属性ahvNo和insuranceNo不会被识别为已更改(不在更新列表中),但它们已更改(在查询中)。

第一个语句成功运行,但第二个语句抛出异常1 of 2 updates failed.

这是我使用的VB代码:

Using dc As New WatchStoreDC()
    Dim usr = If(dc.users.SingleOrDefault(Function(u) u.id = id), New AppUser())
    usr.employeeNo = employeeNo
    usr.ahvNo = dc.formatAHVNumber(ahvNo)
    usr.insuranceNo = dc.formatInsuranceNumber(insuranceNo)
    usr.badgeNo = badgeNo
    usr.birthdate = birthdate
    usr.lastName = lastName
    usr.firstName = firstName
    usr.title = title
    usr.roles = roles

    Dim usrAct = String.Empty, mdfTim = Now
    If usr.id = 0 Then
        usrAct = "ins" : usr.createdAt = mdfTim : usr.createdByID = GeneralInfo.userID : dc.users.InsertOnSubmit(usr) : dc.SubmitChanges()
    Else
        usrAct = "upd" : usr.modifiedAt = mdfTim : usr.modifiedByID = GeneralInfo.userID
    End If

    Dim prf = If(usr.profile, New AppProfile())
    prf.languageID = profileLanguage
    prf.cultureID = profileCulture
    prf.updatedAt = mdfTim

    If prf.id = Guid.Empty Then prf.id = usr.securityID : dc.profiles.InsertOnSubmit(prf)
    dc.SubmitChanges()
End Using

这是LinqToSQL架构:

<Table Name="app.tblUsers" Member="users">
    <Type Name="AppUser">
        <Column Name="usrID" Member="id" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
        <Column Name="usrSecurityID" Member="securityID" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" IsDbGenerated="true" CanBeNull="false" />
        <Column Name="usrCompanyCode" Member="companyCode" Type="System.String" DbType="NVarChar(25)" CanBeNull="true" />
        <Column Name="usrEmployeeNo" Member="employeeNo" Type="System.String" DbType="Char(6)" CanBeNull="true" />
        <Column Name="usrAHVNo" Member="ahvNo" Type="System.String" DbType="Char(14)" CanBeNull="true" />
        <Column Name="usrInsuranceNo" Member="insuranceNo" Type="System.String" DbType="Char(16)" CanBeNull="true" />
        <Column Name="usrTitle" Member="title" Type="System.String" DbType="VarChar(10)" CanBeNull="true" />
        <Column Name="usrLastName" Member="lastName" Type="System.String" DbType="NVarChar(50) NOT NULL" CanBeNull="false" />
        <Column Name="usrFirstName" Member="firstName" Type="System.String" DbType="NVarChar(50)" CanBeNull="true" />
        <Column Name="usrRole" Member="rolesInt" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
    </Type>
</Table>

这是生成的SQL:

UPDATE [app].[tblUsers]
SET [usrModifiedBy] = @p14, [usrModifiedAt] = @p15
WHERE ([usrID] = @p0) AND ([usrSecurityID] = @p1) AND ([usrCompanyCode] IS NULL) AND ([usrEmployeeNo] IS NULL) AND ([usrAHVNo] IS NULL) AND ([usrInsuranceNo] IS NULL) AND ([usrTitle] IS NULL) AND ([usrLastName] = @p2) AND ([usrFirstName] = @p3) AND ([usrLanguage] IS NULL) AND ([usrBirthdate] IS NULL) AND ([usrAddressStreetNo] = @p4) AND ([usrAddressAddLine] IS NULL) AND ([usrAddressZIP] = @p5) AND ([usrAddressCity] = @p6) AND ([usrAddressCountry] = @p7) AND ([usrPhoneWork] IS NULL) AND ([usrPhoneHome] = @p8) AND ([usrPhoneMobile] IS NULL) AND ([usrEMailWork] IS NULL) AND ([usrLogin] IS NULL) AND ([usrSiteLocation] IS NULL) AND ([usrSiteNo] IS NULL) AND ([usrUnitCenter] IS NULL) AND ([usrDepartment] IS NULL) AND ([usrOE] IS NULL) AND ([usrOEName] IS NULL) AND ([usrCostCenter] IS NULL) AND ([usrCCResponsible] IS NULL) AND ([usrSuperior] IS NULL) AND ([usrJoinDate] IS NULL) AND ([usrExitDate] IS NULL) AND ([usrExitReason] IS NULL) AND ([usrBadgeNo] IS NULL) AND ([usrContractType] IS NULL) AND ([usrContractTypeName] IS NULL) AND ([usrPayrollContact] IS NULL) AND ([usrHRContact] IS NULL) AND ([usrApprovals] = @p9) AND ([usrPaymentType] = @p10) AND ([usrRole] = @p11) AND ([usrEMailContact] IS NULL) AND ([usrPhoneContact] IS NULL) AND ([usrCreatedBy] = @p12) AND ([usrCreatedAt] = @p13) AND ([usrModifiedBy] IS NULL) AND ([usrModifiedAt] IS NULL) AND ([usrSignature] IS NULL) AND ([usrSignatureMIME] IS NULL)

UPDATE [app].[tblUsers]
SET [usrModifiedBy] = @p15, [usrModifiedAt] = @p16
WHERE ([usrID] = @p0) AND ([usrSecurityID] = @p1) AND ([usrCompanyCode] IS NULL) AND ([usrEmployeeNo] IS NULL) AND ([usrAHVNo] = @p2) AND ([usrInsuranceNo] IS NULL) AND ([usrTitle] IS NULL) AND ([usrLastName] = @p3) AND ([usrFirstName] = @p4) AND ([usrLanguage] IS NULL) AND ([usrBirthdate] IS NULL) AND ([usrAddressStreetNo] = @p5) AND ([usrAddressAddLine] IS NULL) AND ([usrAddressZIP] = @p6) AND ([usrAddressCity] = @p7) AND ([usrAddressCountry] = @p8) AND ([usrPhoneWork] IS NULL) AND ([usrPhoneHome] = @p9) AND ([usrPhoneMobile] IS NULL) AND ([usrEMailWork] IS NULL) AND ([usrLogin] IS NULL) AND ([usrSiteLocation] IS NULL) AND ([usrSiteNo] IS NULL) AND ([usrUnitCenter] IS NULL) AND ([usrDepartment] IS NULL) AND ([usrOE] IS NULL) AND ([usrOEName] IS NULL) AND ([usrCostCenter] IS NULL) AND ([usrCCResponsible] IS NULL) AND ([usrSuperior] IS NULL) AND ([usrJoinDate] IS NULL) AND ([usrExitDate] IS NULL) AND ([usrExitReason] IS NULL) AND ([usrBadgeNo] IS NULL) AND ([usrContractType] IS NULL) AND ([usrContractTypeName] IS NULL) AND ([usrPayrollContact] IS NULL) AND ([usrHRContact] IS NULL) AND ([usrApprovals] = @p10) AND ([usrPaymentType] = @p11) AND ([usrRole] = @p12) AND ([usrEMailContact] IS NULL) AND ([usrPhoneContact] IS NULL) AND ([usrCreatedBy] = @p13) AND ([usrCreatedAt] = @p14) AND ([usrModifiedBy] IS NULL) AND ([usrModifiedAt] IS NULL) AND ([usrSignature] IS NULL) AND ([usrSignatureMIME] IS NULL)

1 个答案:

答案 0 :(得分:0)

在谷歌搜索了一些后,我发现another question解决了我的问题。

问题如下: 我正在根据这些属性生成哈希码。

我现在添加了一个私有变量来存储第一个生成的哈希码。