'OData Detla<Entity>Patch not updating
I'm currently trying to hack Odata when updating.
I have the following model:
public class Patient
{
[DataMember]
[JsonPropertyName("id")]
public string Id { get; set; }
[DataMember]
[JsonPropertyName("name")]
public string? Name { get; set; }
[DataMember]
[JsonPropertyName("lastname")]
public string? LastName { get; set; }
[DataMember]
[JsonPropertyName("dateofbirth")]
public DateTime DateBirth { get; set; }
}
On the other hand, I have this controller:
[Route("api/v1/patient")]
public class PatientController:ODataController
{
List<Patient> patients = new List<Patient>()
{
new Patient()
{
Id = "1",
Name = "A name",
LastName = "A lastname"
},
new Patient()
{
Id = "2",
Name = "A second name",
LastName = "A second lastname"
}
};
[EnableQuery]
[HttpGet]
public async Task<IActionResult> GetAll()
{
return Ok(patients);
}
[EnableQuery]
[HttpGet("{id}")]
public async Task<IActionResult> Get([FromODataUri]string id)
{
Patient p = patients.SingleOrDefault(p => p.Id == id) ?? null;
if (p is null)
{
return NotFound();
}
return Ok(p);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]Patient patient)
{
var x = patients;
x.Add(patient);
return Ok(x);
}
[EnableQuery]
[HttpPatch("{id}")]
public async Task<IActionResult> Patch([FromODataUri] string id, [FromBody]Delta<Patient> patient)
{
if (!ModelState.IsValid)
{
throw new System.Web.Http.HttpResponseException(System.Net.HttpStatusCode.BadRequest);
}
Patient patientToEdit = patients.SingleOrDefault(p => p.Id == id) ?? null;
if (patientToEdit is not null)
{
patient.Patch(patientToEdit);
return Ok(patientToEdit);
}
else
{
return NotFound(patient);
}
}
}
However, the entity is not updating when calling with the following:
Json with name only and response not updating
The Program.cs file looks like the following:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers().AddOData(options => options.Select().Expand().Filter().OrderBy().SetMaxTop(100).Count());
builder.Services.AddODataQueryFilter();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseRouting();
app.UseODataBatching();
app.UseODataQueryRequest();
app.UseODataRouteDebug();
app.Run();
The Delta patient is showing an empty list of changes when using patient.GetChangedPropertyNames();
Do you know how can I tackle this so I could perform partial patching?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
