私のリポジトリには、2つのテーブルを更新するこの非同期メソッドがあります。テーブルは異なるスキーマにあり、したがって2つのコンテキストになります。コンテキストは、このリポジトリ内のクラス変数です。このメソッドは、Angularクライアントアプリケーションによって呼び出されるwebapiから呼び出されます。私は両方の挿入が成功するか両方が失敗することを保証する必要があります。複製されたレコードをどちらのコンテキストでも挿入しようとすると、重複していないレコードが渡されても、サーバーは常にそのレコードを覚えます。サーバーは、ホスティングサーバーが再起動されるまで失敗を返します。誰もトランザクションスコープでこれを処理する方法を知っていますか?リポジトリで非同期メソッド内でトランザクションスコープを使用する方法
public class RepositoryAsync: IRepositoryAsync {
//CareMgmtTool schema context
private CareMgmtToolContext cmtCtx = new CareMgmtToolContext();
//App schema context
private AppContext appCtx = new AppContext();
//UserProfile schema
private UserProfileContext userProfileCtx = new UserProfileContext();
public async Task <int> PostUserFSR(int userId, int fsrId) {
//poor man's transaction scope
bool operation1Success, operation2Success = false;
UserProfile_UserFacilityServiceRole vm1 = new UserProfile_UserFacilityServiceRole();
vm1.UserId = userId;
vm1.FacilityServiceRoleId = fsrId;
UserProfile_UserFacilityServiceRole ufsr_UserProfileSchema = userProfileCtx
.UserProfile_UserFacilityServiceRole.Add(vm1);
operation1Success = true;
App_SUserFacilityServiceRole vm2 = new App_SUserFacilityServiceRole();
vm2.UserId = userId;
vm2.FacilityServiceRoleId = fsrId;
App_SUserFacilityServiceRole ufsr_appSchema = appCtx.App_SUserFacilityServiceRole.Add(vm2);
operation2Success = true;
if (operation1Success && operation2Success) {
await userProfileCtx.SaveChangesAsync();
await appCtx.SaveChangesAsync();
}
return ufsr_UserProfileSchema.UserFacilityServiceRoleId; //record key
}
}