Reputation: 99
My entity looks as follows:
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; }
}
And the method by which i want to update is:
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);
}
}
What is the equivalent of InsertAsync
when I want to update an existing entity? Is there an UpdateAsync
equivalent method?
Upvotes: 4
Views: 35484
Reputation: 477
Try this if you're using .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;
}
}
Upvotes: 1
Reputation: 149538
Updating an entity in Entity Framework requires you to retrieve the record, update it and then save changes. It will look roughly like this:
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);
}
}
Upvotes: 11