【问题标题】:Entity Framework 6, Repository Pattern, Unit of Work, Save and get last ID and then update recordEntity Framework 6, Repository Pattern, Unit of Work, Save and get last ID 然后更新记录
【发布时间】:2019-02-21 10:58:52
【问题描述】:

我接手了一个使用 EF6、存储库模式、工作单元的 MVC Web 项目。我刚开始学习 EF6。

我想创建一个新组织并将其保存到数据库中,然后获取新的组织 ID。新的组织 ID 将用于重命名将存储在 organization.logo 中的图像文件。然后使用新文件名更新数据库中的组织徽标字段。

我保存了组织,获取了新的组织 ID,重命名了文件,但我无法更新徽标字段?

代码如下:

组织控制器 - 显示基本代码 我已经删除了很多验证和重命名图像文件。

public ActionResult Create(OrganisationViewModel viewModelToCreate)
    {

        Organisation newOrganisation = new Organisation();
        newOrganisation.Name = viewModelToCreate.Name;
        newOrganisation.Logo = "To Change";

        _Uow.OrganisationRepository.InsertOrUpdate(newOrganisation);

        if (ModelState.IsValid)
        {
            _Uow.Save();
            int newOrganisationId = _Uow.OrganisationRepository.LastInsertedID();
            Organisation organisationToUpdate = _Uow.OrganisationRepository.Find(newOrganisationId);
string fileName = newOrganisationId.ToString() + Path.GetExtension(viewModelToCreate.FileNameToAdd.FileName);

                organisationToUpdate.Logo = fileName;
                _Uow.OrganisationRepository.InsertOrUpdate(organisationToUpdate);
                _Uow.Save();
            }

    }


public virtual T InsertOrUpdate(T e)
    {
        DbSet<T> dbSet = Context.Set<T>();

        DbEntityEntry<T> entry;
        if (e.ID != default(int))
        {
            entry = Context.Entry(e);

        }
        else
        {
            T instance = dbSet.Create();
            instance.ID = e.ID;
            entry = Context.Entry(instance);
            dbSet.Attach(instance);
            entry.CurrentValues.SetValues(e);
            e = instance;
        }

        entry.State = e.ID == default(int) ?
                                EntityState.Added :
                                EntityState.Modified;

        return e;
    }


     int IUnitOfWork.Save()
        {

                return _Context.SaveChanges();
     }

      public int LastInsertedID()
         {
             Context = new aContext();
             int newOrganisationId = Context.Organisations.OrderByDescending(x => x.ID).FirstOrDefault().ID;
             return newOrganisationId;
    }

如何使用新文件名更新 organizationToUpdate.Logo,它不会保存到数据库中?

【问题讨论】:

  • 为什么不设置newOrganisation.Logo
  • 我试过了,还是不行。
  • 什么不起作用?很难想象这会插入一个没有徽标的Organisation
  • 更新我的代码以回复@GertArnold 谢谢。
  • 能否展示repo.InsertOrUpdate和uow.Save的代码?

标签: entity-framework-6 repository-pattern unit-of-work


【解决方案1】:

由于您尚未共享您的 UoW 和 Repository 方法,我们无法知道您的代码中究竟发生了什么。但是,我提供了所需的代码来满足您的需要,而无需考虑存储库和 UoW。只需检查您的代码是否正在执行以下操作:

var db = new AppDbContext();

// inserting new organization
Organisation newOrganisation = new Organisation();
newOrganisation.Name = viewModelToCreate.Name;
newOrganisation.Logo = "To Change";
db.Organisation.Add(newOrganisation);
db.SaveChanges();

// updating the inserted organization
string fileName = ”New FileName”;
newOrganisation.Logo = fileName;
db.SaveChanges();

【讨论】:

  • 抱歉,我已经更新了原始问题中的代码。
【解决方案2】:

您需要两次调用 [UOW.Save] 方法。像你一样打电话给第一个。完成此操作后,您保存的实体将使用在数据库中分配的 ID 进行更新。然后添加代码,该代码采用该 ID 并根据需要将其附加到您的文件名中。如果这不起作用,则说明您的 UOW 或存储库有问题,导致其无法正常工作。顺便说一句,UOW 和 Repository 是“模式”而不是实现,所以我们不能期望知道代码是什么样子以及它是如何工作的,如果你想要这个问题,你需要更具体地了解你在做什么回答了

【讨论】:

  • 抱歉,我已经更新了原始问题中的代码
猜你喜欢
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多