c# - 使用 UpdateAsync 方法 ASP.NET Entity Framework

我的实体如下所示:

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 等效方法?

最佳答案

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

https://stackoverflow.com/questions/32376692/

相关文章:

r - 在 R 中将字符串拆分为固定长度元素的最快方法

c# - 如何使用 LinQ 按值分组获取字典中的键列表

date - 同步系统时间和日期的批处理文件

java - 覆盖哈希码方法时获取 "Stack overflow error "

oracle - 12c 和 ORA-01792 : maximum number of colum

html - 使用 ng-classes 样式输入占位符颜色样式

symfony - 如何使用 symfony 2 使工作软删除和唯一实体

shopify - Liquid 中的逗号分隔列表

amazon-web-services - AWS : How do I programmatica

css - 选择器属性中的手写笔迭代