Reputation: 142
In the following code, I have wrapped a dbContextTransactions around multiple savechanges. Everything from the ViewModel contains the correct data but for some reason, EF does not save the changes. When I debug it, I'm not seeing any exception thrown as well. Is there something special I need to do for a transaction that would prevent this from working?
I have validated that I am indeed targeting the correct database as I can query it through another page successfully.
public static Logger logger = LogManager.GetCurrentClassLogger();
public static Guid AddPlanItem(PlanItemAddViewModel viewModel, IEnumerable<HttpPostedFileBase> images)
{
Guid planIdGUID = Guid.NewGuid();
using (var context = new ApplicationDbContext())
{
using (var dbContextTransaction = context.Database.BeginTransaction())
{
try
{
PlanItem planItem = context.Plans.Create();
planItem.PlanId = planIdGUID;
planItem.CreateDate = DateTime.Now;
planItem.PlanIdTitle = viewModel.PlanIdTitle;
planItem.PlanTitle = viewModel.PlanTitle;
planItem.SolutionTitle = viewModel.SolutionTitle;
planItem.ActivityCodeId = viewModel.ActivityCodeId;
planItem.RegionId = viewModel.RegionId;
planItem.OperatingCenterId = viewModel.OperatingCenterId;
planItem.DistrictId = viewModel.DistrictId;
planItem.ServiceCenterId = viewModel.ServiceCenterId;
planItem.PlannerRACFId = viewModel.PlannerRACFId;
planItem.SeverityId = viewModel.SeverityId;
planItem.Description = viewModel.Description;
planItem.PreviousPlan = viewModel.PreviousPlan;
context.Plans.Add(planItem);
context.SaveChanges();
foreach (HttpPostedFileBase image in images)
{
var imageSubmit = new PlanItemImage
{
PlanId = planIdGUID,
PlanImageId = Guid.NewGuid(),
Image = image.ConvertToByte(),
ImageTitle = image.FileName
};
context.Image.Add(imageSubmit);
context.SaveChanges();
}
dbContextTransaction.Commit();
}
catch (Exception ex)
{
logger.Error("Error: " + ex.Message);
dbContextTransaction.Rollback();
}
}
}
return planIdGUID;
}
Upvotes: 0
Views: 228
Reputation: 186
For the code I see, I'm not quite sure what the issue could be (yet). Even so, we can take an approach to determine what the issue could be.
First let's simplify the simplify the code.
public static Guid AddPlanItem(PlanItemAddViewModel viewModel, IEnumerable<HttpPostedFileBase> images)
{
Guid planIdGUID = Guid.NewGuid();
using (var context = new ApplicationDbContext())
{
PlanItem planItem = context.Plans.Create();
planItem.PlanId = planIdGUID;
planItem.CreateDate = DateTime.Now;
planItem.PlanIdTitle = viewModel.PlanIdTitle;
planItem.PlanTitle = viewModel.PlanTitle;
planItem.SolutionTitle = viewModel.SolutionTitle;
planItem.ActivityCodeId = viewModel.ActivityCodeId;
planItem.RegionId = viewModel.RegionId;
planItem.OperatingCenterId = viewModel.OperatingCenterId;
planItem.DistrictId = viewModel.DistrictId;
planItem.ServiceCenterId = viewModel.ServiceCenterId;
planItem.PlannerRACFId = viewModel.PlannerRACFId;
planItem.SeverityId = viewModel.SeverityId;
planItem.Description = viewModel.Description;
planItem.PreviousPlan = viewModel.PreviousPlan;
context.Plans.Add(planItem);
context.SaveChanges();
}
return planIdGUID;
}
This way we achieve two things:
Second, If everything works fine, we can add the next piece, the images but this time with the save changes outside the for bucle.
foreach (HttpPostedFileBase image in images)
{
var imageSubmit = new PlanItemImage
{
PlanId = planIdGUID,
PlanImageId = Guid.NewGuid(),
Image = image.ConvertToByte(),
ImageTitle = image.FileName
};
context.Image.Add(imageSubmit);
}
context.SaveChanges();
UPDATE
The issue was caused due to the multiple SaveChanges() calls. Editing the code to a single one solve the issue.
Upvotes: 1