我正在編碼一個Web API 2
web服務,並希望在編輯對象之前獲取對象的值在DbSet
中有所幫助。如何在數據庫中編輯對象之前獲取對象的值?
我已經做了一些研究,我相信我需要使用ObjectStateEntry
對象。但是,我不確定如何使用ObjectStateEntry
對象獲取對象的值。
下面是一些代碼的簡單放功能,我認爲應該工作:
// PUT: api/Templates1/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutTemplate(int id, Template template)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != template.id)
{
return BadRequest();
}
ObjectStateEntry myObjectState = db.ObjectStateManager.GetObjectStateEntry(template);
var originalValues = myObjectState.OriginalValues;
db.Entry(template).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TemplateExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
我收到以下錯誤:
'DataService.Context.DataServiceContext' does not contain a definition for 'ObjectStateManager' and no extension method 'ObjectStateManager' accepting a first argument of type 'DataService.Context.DataServiceContext' could be found (are you missing a using directive or an assembly reference?)
是否有人可以幫助我得到的值對象,在數據庫中編輯對象之前?