【问题标题】:Use of UpdateAsync method ASP.NET Entity FrameworkASP.NET Entity Framework 使用 UpdateAsync 方法
【发布时间】:2015-09-03 13:13:52
【问题描述】:

我的实体如下所示:

public class AddPatientReportDentalChartInput : IInputDto
{
    [Required]
    [MaxLength(PatientReportDentalChart.TeethDesc)]
    public string Image { get; set; }

    [Required]
    public virtual int PatientID { get; set; }
    [Required]
    public virtual  int TeethNO { get; set; }
    public string SurfaceDefault1 { get; set; }
    public string SurfaceDefault2 { get; set; }
    public string SurfaceDefault3 { get; set; }
    public string SurfaceDefault4 { get; set; }
    public string SurfaceDefault5 { get; set; }
}

我要更新的方法是:

public async Task addPatientReportDentalChart(AddPatientReportDentalChartInput input)
{
    var pid = input.PatientID;
    var chartdetails = _chartReportRepository
                        .GetAll()
                        .WhereIf(!(pid.Equals(0)),
                                   p => p.PatientID.Equals(pid)).ToList();

    if (chartdetails.Count>0)
    {
        //Update should be apply here 
        //please suggest me the solution using updatesync
    }
    else 
    { 
        var patientinfo = input.MapTo<PatientReportDentalChart>();
        await _chartReportRepository.InsertAsync(patientinfo);
    }
}

当我想更新现有实体时,InsertAsync 的等价物是什么?有没有UpdateAsync 等效的方法?

【问题讨论】:

  • 我只想使用 updateSync 方法更新数据库,例如 Inserting await _chartReportRepository.InsertAsync(patientinfo);
  • 使用FindAsync查找实体,修改它,然后使用SaveChangesAsync
  • 你能推荐我上面的代码吗?
  • 在下面查看我的答案。

标签: c# entity-framework asp.net-mvc-4 entity-framework-5 entity-framework-6


【解决方案1】:

在 Entity Framework 中更新实体需要您检索记录、更新它然后保存更改。它大致如下所示:

public async Task AddPatientReportDentalChartAsync(AddPatientReportDentalChartInput input)
{
    var pid = input.PatientID;
    var chartdetails = _chartReportRepository
                        .GetAll()
                        .WhereIf(!(pid.Equals(0)),
                                   p => p.PatientID.Equals(pid)).ToList();

    if (chartdetails.Count > 0)
    {
        var entity = await _chartReportRepository
                                .YourTableName
                                .FindAsync(entity => entity.SomeId == matchingId);

        entity.PropertyA = "something"
        entity.PropertyB = 1;
        await _chartReportRepository.SaveChangesAsync();
    }
    else 
    { 
        var patientinfo = input.MapTo<PatientReportDentalChart>();
        await _chartReportRepository.InsertAsync(patientinfo);
    }
}

【讨论】:

  • 非常感谢您的回答。你救了我一整天。谢谢你:)
【解决方案2】:

如果您使用的是 .NET CORE 3.1,请尝试此操作

 public async Task<int> UpdateChat(MChat mChat)
    {
        try
        {
            return await Task.Run(() =>
            {
                BDContext.Chat.Update(new Chat
                {
                    Id = mChat.id,
                    UsuarioIdInicia = mChat.usuarioIdInicia,
                    UsuarioIdFinaliza = mChat.usuarioIdFinaliza,
                    EstadoChatId = mChat.estadoChatId
                });
                return BDContext.SaveChanges();
            });
        }
        catch (Exception ex)
        {
            Console.WriteLine(Constantes.ERROR_DETECTADO + ex.InnerException.ToString());
            return Constantes.ERROR_1;
        }

    }

【讨论】:

  • 请不要这样做。您只需通过调用同步BDContext.SaveChanges() 来阻塞线程。如果您真的想实现它,请使用.SaveChangesAsync()而不使用Task.Run
  • 我的朋友 Task.Run 允许你等待......这意味着它不会阻塞主线程,因为它不能同步工作......我本可以使用 .SaveChangesAsync(),但我宁愿有整个逻辑异步运行,以防我需要添加更多逻辑......
  • @JohnDoe 是对的,你这样做只是在丢掉一个线程。只需在 SaveChangesAsync 上使用 await 即可在“阻塞主线程”方面为您提供相同的结果,但无需使用 2 个线程来执行此操作。
猜你喜欢
  • 1970-01-01
  • 2020-03-29
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 2019-06-27
  • 2020-09-16
相关资源
最近更新 更多