SaveChangesAsync() method not updating database

.net c# entity-framework entity-framework-6

Question

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
0
6/8/2018 2:38:54 PM

Accepted Answer

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.

0
6/11/2018 1:21:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow