public ResultM UploadFile(FileInfoM pFileInfoM)
{
ResultM result = new ResultM() { Flag = };
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 = ; result.Msg = "不支持此文件类型!";
return result;
}
var recordM = db.Record.SingleOrDefault(t => t.ReferenceNumber == pFileInfoM.ReferenceNumber && t.RecordTypeId == );
if (recordM == null)
{
result.Flag = ; result.Msg = "找不到ReferenceNumber:" + pFileInfoM.ReferenceNumber + "对应记录!";
return result;
}
if (pFileInfoM.FileData.Length <= )
{
result.Flag = ; 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 = ,//select DocMgtFolderId from RefDocMgtFolder where DocMgtFolderName='Documents' = 1
AuditUserId = pFileInfoM.AuditUserId
});
//Create DocMgtUserAccessControl
for (int i = ; i <= ; i++)
{
db.DocMgtUserAccessControl.Add(new DocMgtUserAccessControl()
{
FileId = fileInfoModel.FileId,
UserTypeId = i,//UserTypeId = RefUserType.UserTypeId
AuditUserId =
});
}
db.SaveChanges();
tranDB.Commit();
tranDBDoc.Commit();
}
catch (Exception ex)
{
Log.Writer(ex, "UploadFile");
tranDB.Rollback();
tranDBDoc.Rollback();
result.Flag = ; result.Msg = ex.ToString();
}
finally
{
tranDB.Dispose();
tranDBDoc.Dispose();
}
return result;
}
用using Database.BeginTransaction() 可只写Commit()可不写Rollback()