I have the following straightforward case where a client communicates with .NET Web API:
- Client makes a request for an object to the Web API giving the requested ID.
- Web API returns the object to the client.
- Client changes several properties of the object.
- Client sends back the object (not the changed properties) to the Web API using PUT method.
- Web API has to use Entity Framework to update the database.
I am not able to get step 5 implemented.
Here is the code I am using:
[HttpPut]
[ActionName("Update")]
public IHttpActionResult Update(MyModel myModel)
{
this.context.MyModel.Attach(myModel);
this.context.SubmitChanges();
return Ok();
}
This code does not work.
We also know that the endpoints of WebAPI are stateless.
I have seen other solutions like this:
[HttpPut]
[ActionName("Update")]
public IHttpActionResult Update(MyModel myModel)
{
this.context.MyModel.Add(myModel);
this.context.Entry(myModel).State = System.Data.Entity.EntityState.Modified;
this.context.SaveChanges();
return Ok();
}
But the problem with this code is that I am using System.Data.Linq.DataContext
which does not have the property Entry
.
Is there a way to do that using System.Data.Linq.DataContext
?