本文整理汇总了C#中DBContext.UseTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# DBContext.UseTransaction方法的具体用法?C# DBContext.UseTransaction怎么用?C# DBContext.UseTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBContext
的用法示例。
在下文中一共展示了DBContext.UseTransaction方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadFile
public void UploadFile(DetailProblem problem)
{
logger.DebugFormat("Begin UploadFile , WorkZoneId = {0}, workZOneDetailId id = {1}", problem.WorkZoneId, problem.WorkZoneDetailId);
WorkZone wk = WorkZone.GetWorkZone(problem.WorkZoneId);
string pathFolderProblem = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, Common.GetFolderWorkZoneProblem(wk.CompanyId, wk.CompanyName, wk.FactoryId, wk.FactoryName, wk.WorkZoneId, wk.Name));
logger.Debug("PhysicalPath pathFolder =" + pathFolderProblem);
string uploadFilePath = "";
try
{
using (DBContext db = new DBContext())
{
using (System.Data.Common.DbTransaction tran = db.UseTransaction())
{
try
{
if (problem.FileId == 0) // case insert New
{
logger.Debug(" Case Insert new problem");
WorkZoneDetail d = WorkZoneDetail.GetWorkZoneDetail(problem.WorkZoneId, problem.WorkZoneDetailId);
d.Status = 1; // Has Problem
d.ModifiedAccount = problem.CreateAccount;
d.Update();
problem.FileId = DetailProblem.GetNextFileID(d.WorkZoneId, d.WorkZoneDetailId);
if(problem.Base64Data!= null)
{
problem.ImageFile = string.Format("{0}_{1}.png", d.WorkZoneDetailId, problem.FileId);
}
problem.ModifiedAccount = problem.CreateAccount;
problem.Insert();
logger.Debug(" Update problem and WorkZone Detail");
if (!Directory.Exists(pathFolderProblem))
Directory.CreateDirectory(pathFolderProblem);
if (!string.IsNullOrEmpty(problem.Base64Data))
{
uploadFilePath = Path.Combine(pathFolderProblem, problem.ImageFile);
logger.Debug("Case Create file problem = " + uploadFilePath);
Byte[] data = Convert.FromBase64String(problem.Base64Data);
File.WriteAllBytes(uploadFilePath, data);
problem.Base64Data = null;
}
}
else if (problem.FileId != 0) // Update or Delete
{
DetailProblem p = DetailProblem.GetDetailProblem(problem.WorkZoneId, problem.WorkZoneDetailId, problem.FileId);
if(string.IsNullOrEmpty(problem.ImageFile))
{
//delete
logger.Debug("Case Delete detail Problem");
p.Delete();
uploadFilePath = Path.Combine(pathFolderProblem, p.ImageFile);
logger.Debug("Case Delete file uploadFilePath = " + uploadFilePath);
if (File.Exists(uploadFilePath))
File.Delete(uploadFilePath);
}
else
{
logger.Debug("Case update detail Problem");
p.Comment = problem.Comment;
p.ModifiedAccount = problem.CreateAccount;
p.Update();
}
}
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
logger.Error("Error UploadFile", ex);
throw ex;
}
}
}
logger.Debug("End UploadFile");
}
catch (Exception ex)
{
logger.Error("Error UploadFile ", ex);
throw ex;
}
}