SaveChangesAsync()方法不更新数据库 [英] SaveChangesAsync() method not updating database

查看:1163
本文介绍了SaveChangesAsync()方法不更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序具有:

  • 实体框架 6.1.3
  • .NET Framework 4.5.1

我将.NET Framework从版本 4.5.1 升级到了 4.6.2 .

因此,"System.Data.Entity"方法的"SaveChangesAsync()"不再更新我的数据库.

我找不到是由于.NET Framework程序包还是Entity Framework 6.1.3与.NET Framwork 4.6.2之间的"SaveChangesAsync()"方法之间的某些兼容性问题而导致的...我迷路了!/p>

以下是一些示例:

服务等级

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类

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>

我已经检查了异步/等待编排,一切似乎都正确. 那在那里发生了什么?

谢谢.

解决方案

我终于发现自己在做错什么. 实际上,我正在使用"Unity"(版本5.8.6).我上次与.NET Framework同时更新了Unity软件包.

我这样制作了UnityConfig.cs:

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

错误与管理器的类型有关.正确的是 PerRequestLifetimeManager :

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

我对 PerRequestLifetimeManager 的理解是,容器将解析http请求中对象的同一实例,而 PerResolveLifetimeManager 将标记类型,以便实例可在整个构建对象图中重用.

总而言之,对于 PerRequestLifetimeManager ,在不同的http请求中,我们将具有不同的已解析对象.

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.

解决方案

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.

这篇关于SaveChangesAsync()方法不更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆