LinqToSQLオブジェクトを更新しようとすると、エンジンはDataContext.getChangeSet().Updates
変更したプロパティごとに1つのエントリを作成します。上書きチェックが原因でこれは明らかに失敗します...
この動作を修正するにはどうすればよいですか(すべての変更に対して1つの更新)?
生成されたSQLコードでわかるように、プロパティahvNoとinsuranceNoは変更されたものとして認識されません(更新リストにはありません)が、変更されています(whereクエリで)。
最初のステートメントは正常に実行されますが、2番目のステートメントは例外をスローします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)