I want to implement Transcation,commit and roll back but in first table data not removed after getting time out ,which break point goes to exception block, and any standard way to write this api method
async Task IMaterialManager.AddVersionDetails(AddVersionData AppItem)
{
string Action = string.Empty;
_ResponseStatus = new ResponseStatus();
var IsValidFile = false;
var FileNameByUser = String.Empty;
var FileExtension = String.Empty;
var FileUniqueName = String.Empty;
var FileBase64 = String.Empty;
var FileOriginalNameWithPath = String.Empty;
using (var context = new LmsDocumentManagerContext())
{
using (var dbContextTransaction = context.Database.BeginTransaction())
{
try
{
var createDate = DateTime.UtcNow.Add(new TimeSpan(+5, +30, +00));
var MaxVersion = (from VersionDetails in _dbContext.VersionDetails.GroupBy(x => x.ContentId).Select(k => new { ContentId = k.Key, VersionName = k.Max(p => p.VersionName) })
where VersionDetails.ContentId == AppItem.ContentId
select new
{
ContentId = VersionDetails.ContentId,
VersionName = VersionDetails.VersionName
});
var ContentDetailsItem = _dbContext.ContentDetails.Where(k => k.Id == AppItem.ContentId).FirstOrDefault();
if (ContentDetailsItem != null)
{
var Item = new VersionDetails();
if (AppItem.FileTypeId == (int)FileType.Document)
{
IsValidFile = IsValidUpload(AppItem.FileNameByUserBase).Result;
if (IsValidFile)
{
FileNameByUser = AppItem.FileNameByUser;
FileExtension = Path.GetExtension(AppItem.FileNameByUser);
FileUniqueName = GetUniqueFileName(FileNameByUser, FileExtension, MaterialType.StudyMatetial.ToString());
FileBase64 = AppItem.FileNameByUserBase;
FileOriginalNameWithPath = Path.Combine(_DocumentPath.UploadPathStudyMaterial, FileUniqueName);
Base64ToOriginalFile(FileBase64, FileOriginalNameWithPath);
}
}
int IncrementedVersion = (Convert.ToInt32(MaxVersion.FirstOrDefault().VersionName) + 1);
Item.ContentId = AppItem.ContentId;
Item.VersionName = Convert.ToString(IncrementedVersion);
Item.FileNameByUser = FileNameByUser;
Item.FilePhysicalDirectoryId = FileUniqueName;
if (AppItem.FileTypeId == (int)FileType.Link)
{
Item.FilePath = AppItem.Link;
}
if (AppItem.FileTypeId == (int)FileType.Document)
{
Item.FilePath = FileOriginalNameWithPath;
}
Item.VersionNote = AppItem.VersionNote;
Item.FilePhysicalDirectoryId = FileUniqueName;
Item.CreatedBy = _authenticationManager.GetAuthenticatedUser().Result.UserId;
Item.CreatedOn = createDate;
_dbContext.VersionDetails.Add(Item);
var result = await _dbContext.SaveChangesAsync();
var LatestVersionId = _dbContext.VersionDetails.OrderByDescending(u => u.VersionId).FirstOrDefault();
if (result > 0)
{
ContentDetailsItem.FileType = AppItem.FileType;
ContentDetailsItem.FileTypeId = AppItem.FileTypeId;
ContentDetailsItem.LatestVersionId = LatestVersionId.VersionId;
ContentDetailsItem.LatestVersionName = Convert.ToString(IncrementedVersion);
if (AppItem.FileTypeId == (int)FileType.Link)
{
ContentDetailsItem.LatestFileNameByUser = "";
ContentDetailsItem.LatestFilePhysicalDirectoryId = "";
ContentDetailsItem.LatestFilePath = AppItem.Link;
}
if (AppItem.FileTypeId == (int)FileType.Document)
{
ContentDetailsItem.LatestFileNameByUser = AppItem.FileNameByUser;
ContentDetailsItem.LatestFilePhysicalDirectoryId = FileUniqueName;
ContentDetailsItem.LatestFilePath = FileOriginalNameWithPath;
}
ContentDetailsItem.FileTypeId = AppItem.FileTypeId;
ContentDetailsItem.ModifiedOn = createDate;
ContentDetailsItem.ModifiedBy = _authenticationManager.GetAuthenticatedUser().Result.UserId;
_dbContext.ContentDetails.Update(ContentDetailsItem);
#region ActionLog
var AddActionLog = new ActionLog();
var MaxVersionId = (from VersionDetails in _dbContext.VersionDetails.GroupBy(x => x.VersionId).Select(k => new { VersionId = k.Key, ContentId = k.Max(p => p.ContentId) })
where VersionDetails.ContentId == Item.ContentId
select new
{
VersionId = VersionDetails.VersionId,
ContentId = VersionDetails.ContentId ?? 0
}).OrderByDescending(k => k.VersionId).FirstOrDefault();
var _ContentTopic = (from ContentTopic in _dbContext.ContentDetails.Where(k => k.Id == MaxVersionId.ContentId)
select new
{
ContentTopic.Topic
}).FirstOrDefault();
var _ContentVersionName = (from ContentVersionName in _dbContext.VersionDetails.Where(k => k.VersionId == MaxVersionId.VersionId)
select new
{
ContentVersionName.VersionName
}).FirstOrDefault();
AddActionLog.ContentId = MaxVersionId.ContentId;
AddActionLog.VersionId = MaxVersionId.VersionId;
AddActionLog.Action = "New Content Upload";
AddActionLog.Comment = _ContentTopic.Topic + "(Version:-" + _ContentVersionName.VersionName + ")";
AddActionLog.CreatedBy = _authenticationManager.GetAuthenticatedUser().Result.UserId;
AddActionLog.CreatedOn = createDate;
_dbContext.ActionLog.Add(AddActionLog);
#endregion
}
else
{
_ResponseStatus.Status = (int)HttpStatusCode.InternalError;
_ResponseStatus.ErrorMessage = ErrorMsg.InternalServerError;
}
}
else
{
_ResponseStatus.Status = (int)HttpStatusCode.NotValid;
_ResponseStatus.ErrorMessage = ErrorMsg.ContentNotExist;
}
var result1 = await _dbContext.SaveChangesAsync();
if (result1 > 0)
{
_ResponseStatus.Status = (int)HttpStatusCode.Sucess;
_ResponseStatus.ErrorMessage = SuccessMsg.VersionAddMsg;
}
else
{
_ResponseStatus.Status = (int)HttpStatusCode.InternalError;
_ResponseStatus.ErrorMessage = ErrorMsg.InternalServerError;
}
dbContextTransaction.Commit();
}
catch (Exception ex)
{
dbContextTransaction.Rollback();
}
}
}
return _ResponseStatus;
}
Jignesh KumarPosted Feb 15, 2023, 8:19 AM
Hello,
Please check this one, I think you are using two different dbcontext,
first one you have created context in this line,
using (var context = new LmsDocumentManagerContext())
in below code context is difference (_dbContext.VersionDetails),
both place it should be same context then transaction will work.
Sandeep KumarPosted Feb 15, 2023, 8:38 AM
Sir can u tell me my Code format is ok ,i mean standard ar anything that i need to change