EF6不保存更改

时间:2018-08-31 19:19:33

标签: c# sql-server entity-framework

在以下代码中,我已将dbContextTransactions包裹在多个savechange周围。 ViewModel中的所有内容都包含正确的数据,但是由于某些原因,EF不会保存更改。当我调试它时,也看不到任何异常。对于交易,我需要做些特别的事情来阻止它吗?

我已经确认我确实以正确的数据库为目标,因为我可以成功地通过另一个页面查询它。

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

1 个答案:

答案 0 :(得分:1)

对于我看到的代码,我还不确定(可能)是什么问题。即使这样,我们也可以采用一种方法来确定问题的根源。

首先让我们简化代码。

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

这样,我们可以实现两件事:

  1. 通过删除try / catch,我们将能够看到任何可能的异常
  2. 通过简化保存逻辑,我们可以确定问题出在哪里。如果使用此代码一切正常,那么我们可以包括保存图像及其行为的片段。

第二,如果一切正常,我们可以添加下一张图像,但是这次保存更改在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();

更新

此问题是由于多次SaveChanges()调用引起的。将代码编辑为一个即可解决问题。