SaveChangesAsync() method not updating database

三世轮回 提交于 2019-12-25 02:15:41

问题


I have a web application with:

  • Entity Framework 6.1.3
  • Framework .NET 4.5.1

I made an upgrade of .NET Framework from version 4.5.1 to 4.6.2.

Since that, the "SaveChangesAsync()" of "System.Data.Entity" method does not updating my database anymore.

I can not find if it's because of the .NET framework package or some compatibility issues between Entity Framework 6.1.3 and .NET Framwork 4.6.2 for "SaveChangesAsync()" method... I'm a lost!

Here's some samples:

Service class

public async Task<MyActionResponseModel> UpdateSomething(Catalogue catalogue)
{
    if (catalogue== null) throw new ArgumentNullException("catalogue is null");
    var response = new ActionAddResponse();

    var catalogueToUpdate = await _catalogueRepository.GetCatalogueById(catalogue.Id);
    var myDate = new DateTime();
    if (catalogue.EndDate != null)
    {
        myDate = catalogue.EndDate.Value;
        myDate = myDate.Date.AddHours(23).AddMinutes(59);
    }

    catalogueToUpdate.Year = catalogue.Year;
    catalogueToUpdate.StartDate = catalogue.StartDate;
    catalogueToUpdate.EndDate = catalogue.EndDate!= null ? myDate : catalogue.EndDate;
    catalogueToUpdate.Tax = catalogue.Tax;
    catalogueToUpdate.Online = Convert.ToBoolean(catalogue.Online);
    catalogueToUpdate.RefNote = catalogue.RefNote;

    await _unitOfWork.SaveAsync();

    response.Success = true;
    response.Message = string.Format("Catalogue has been update");

    return response;
}

UnitOfWork class

public class UnitOfWork:IUnitOfWork
{    
    public async Task SaveAsync()
    {
        await _context.SaveChangesAsync(); // System.Data.Entity
    }
}

package.config

<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net462" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="10.0.1" targetFramework="net451" />
</packages>

I already checked my async/await choregraphy and everything seems correct. So what is happening right there?

Thank you in advance.


回答1:


I finally found what I was doing wrong. In fact I'm using "Unity" (verison 5.8.6). I made an update of my Unity package at the same time as the .NET Framework last time.

I made my UnityConfig.cs like this:

public static void ConfigureUnityContainer(IUnityContainer container)
{
    // some other resgistration
    container.RegisterType<MyEntities>(new PerResolveLifetimeManager());
}

The error is on the type of the manager. The correct one is PerRequestLifetimeManager:

public static void ConfigureUnityContainer(IUnityContainer container)
{
    // some other resgistration
    container.RegisterType<MyEntities>(new PerRequestLifetimeManager());
}

What I understood for PerRequestLifetimeManager is that the container will resolve the same one instance of the object in http request while PerResolveLifetimeManager will mark the type so that instances are reused across the build-up object graph.

In summary, with PerRequestLifetimeManager, in different http requests we will have different resolved objects.



来源:https://stackoverflow.com/questions/50762560/savechangesasync-method-not-updating-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!