首页 > 代码库 > Multi-Database Transaction Demo
Multi-Database Transaction Demo
public ResultM UploadFile(FileInfoM pFileInfoM) { ResultM result = new ResultM() { Flag = 1 }; DbModel db = new DbModel(); DbDocModel dbDoc = new DbDocModel(); var tranDB = db.Database.BeginTransaction(); var tranDBDoc = dbDoc.Database.BeginTransaction(); try { //validate var fdmtM = db.RefDocMgtType.FirstOrDefault(t => t.RefDocMgtTypeName == pFileInfoM.DocType); if (fdmtM == null) { result.Flag = 0; result.Msg = "不支持此文件类型!"; return result; } var recordM = db.Record.SingleOrDefault(t => t.ReferenceNumber == pFileInfoM.ReferenceNumber && t.RecordTypeId == 20); if (recordM == null) { result.Flag = 0; result.Msg = "找不到ReferenceNumber:" + pFileInfoM.ReferenceNumber + "对应记录!"; return result; } if (pFileInfoM.FileData.Length <= 0) { result.Flag = 0; result.Msg = "上传文件大小不正确!"; return result; } byte[] bytes = pFileInfoM.FileData; //流转换为byte //byte[] bytes = new byte[pFileInfoM.File.Length]; //pFileInfoM.File.Read(bytes, 0, bytes.Length); //pFileInfoM.File.Seek(0, SeekOrigin.Begin);// 设置当前流的位置为流的开始 //Create DBDocManagement var docManagementModel = new DBDocManagement() { CreateDate = DateTime.Now, Blob = bytes, FileExtension = pFileInfoM.ExtendName }; dbDoc.DBDocManagement.Add(docManagementModel); dbDoc.SaveChanges(); //Create FileInformation FileInformation fileInfoModel = new FileInformation { Title = "Document", FileSize = (new Func<int>(() => { int val; int.TryParse(pFileInfoM.FileSize, out val); return val; })).Invoke(), OriginalFile = pFileInfoM.DocName, AuditUserId = pFileInfoM.AuditUserId, CreatedByUserId = pFileInfoM.AuditUserId, UploadDate = DateTime.Now, FileExtension = pFileInfoM.ExtendName, CorrespondenceDate = DateTime.Now, StatusChangedDate = DateTime.Now, StatusChangedBy = pFileInfoM.AuditUserId, DocMgtStatus = "I", ExternalFileId = docManagementModel.FileId,//DocDb RefDocMgtTypeId = fdmtM.RefDocMgtTypeId, DocDescription = pFileInfoM.DocName.Replace("." + pFileInfoM.ExtendName, ""), Author = pFileInfoM.Author, StatusChangedComment = "Default Comment", Recipient = "" }; db.FileInformation.Add(fileInfoModel); db.SaveChanges(); //Create Link Relationship db.LnkRecordFile.Add(new LnkRecordFile() { RecordId = recordM.RecordID, FileId = fileInfoModel.FileId, DocMgtFolderId = 1,//select DocMgtFolderId from RefDocMgtFolder where DocMgtFolderName=‘Documents‘ = 1 AuditUserId = pFileInfoM.AuditUserId }); //Create DocMgtUserAccessControl for (int i = 1; i <= 3; i++) { db.DocMgtUserAccessControl.Add(new DocMgtUserAccessControl() { FileId = fileInfoModel.FileId, UserTypeId = i,//UserTypeId = RefUserType.UserTypeId AuditUserId = 1 }); } db.SaveChanges(); tranDB.Commit(); tranDBDoc.Commit(); } catch (Exception ex) { Log.Writer(ex, "UploadFile"); tranDB.Rollback(); tranDBDoc.Rollback(); result.Flag = 0; result.Msg = ex.ToString(); } finally { tranDB.Dispose(); tranDBDoc.Dispose(); } return result; }
Multi-Database Transaction Demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。