Multi-Database Transaction Demo - -Ward

发布时间:2017-2-26 6:53:18 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Multi-Database Transaction Demo - -Ward",主要涉及到Multi-Database Transaction Demo - -Ward方面的内容,对于Multi-Database Transaction Demo - -Ward感兴趣的同学可以参考一下。

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;
        }

上一篇:初始化构造函数中定义的实体集合,方便嵌套类型的遍历
下一篇:asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。