Entity Framework 6、Repository Pattern、Unit of Work、Save and get last ID 然后update record

Entity Framework 6, Repository Pattern, Unit of Work, Save and get last ID and then update record

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

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

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

代码如下:

Organisation Controller - 显示了基本代码我已经删除了很多验证并重命名了图像文件。

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

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

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

由于您没有分享您的 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();