Below code is generated from Wep Api Odata v4 scaffolding tool.
PUT Method
public IHttpActionResult Put([FromODataUri] string key, Delta<Product> patch)
{
Validate(patch.GetEntity());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Product product = db.Products.Find(key);
if (product == null)
{
return NotFound();
}
patch.Put(product);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(product);
}
PATCH Method:
[AcceptVerbs("PATCH", "MERGE")]
public IHttpActionResult Patch([FromODataUri] string key, Delta<Product> patch)
{
Validate(patch.GetEntity());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Product product = db.Products.Find(key);
if (product == null)
{
return NotFound();
}
patch.Patch(product);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(product);
}
Rowversion field in model
[Timestamp]
public byte[] RowVersion { get; set; }
Questions:
DbUpdateConcurrencyException
never thrown. Any reason?[ConcurrencyCheck]
what is the use of it? Can I use this?Providing a code sample will be highly appreciated!.
i solve this checking my self the concurrency fields because the patch or put dont does. This is my code where plato.TimeStamp
is the [Timestamp]
property.
public async Task<IHttpActionResult> Put([FromODataUri] int key, Delta<Plato> patch)
{
Validate(patch.GetEntity());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Plato plato = await db.Platos.FindAsync(key);
if (plato == null)
{
return NotFound();
}
//Here save the current value in the DB
string timeStamp = Convert.ToBase64String(plato.TimeStamp);
patch.Put(plato);
try
{
//Here plato.TimeStamp is update from remote, must be equal to stored value
if (timeStamp != Convert.ToBase64String(plato.TimeStamp))
{
throw new DbUpdateConcurrencyException();
}
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PlatoExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(plato);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.