Reputation: 8171
I am trying to update a record with EF in a WebAPI PUT controller. I am using the following code
Product dbProduct = db.Products.FirstOrDefault(s => s.InternalReferenceId == InternalReferenceId && s.SupplierId == SupplierId);
if (dbProduct != null)
{
db.Products.Attach(dbProduct);
var entry = db.Entry(dbProduct);
entry.Property(e => e.Description).IsModified = true;
await db.SaveChangesAsync();
}
else
{
return NotFound();
}
return Ok();
The product is found, as it returns 200 Ok. But nothing gets updated. I am currently just trying to get the description updated.
I am calling api.com/api/products/update?InternalReferenceId=1&SupplierId=1
and it, as mentioned, finds a record. In the PUT request, i have the following
{
"description": "testing 123"
}
Why wont it update?
Upvotes: 0
Views: 143
Reputation: 4974
You're getting the entity and attaching it as it is without assigning new value of Description. Try this with your existing code:
Product dbProduct = db.Products.FirstOrDefault(s => s.InternalReferenceId == InternalReferenceId && s.SupplierId == SupplierId);
if (dbProduct != null)
{
dbProduct.Description = "testing 123";
await db.SaveChangesAsync();
}
else
{
return NotFound();
}
return Ok();
But the best way go would be:
body
of PUT
request with ID
and value of updated column
.[FromBody]
and attach it your context.SaveChanges
.Something like this:
public bool Put([FromBody]Product updatedProduct)
{
db.Products.Attach(updatedProduct);
var entry = db.Entry(updatedProduct);
entry.Property(e => e.Description).IsModified = true;
return db.SaveChanges() > 0;
}
and your JSON would look like:
{
"ProductID": 1, //id of your record to be updated.
"InternalReferenceId": 1,
"SupplierId": 1,
"Description": "testing 123"
}
The approach you want to follow requires the object
of entity with at least its original ID
of database so that you can directly attach it to your dbContext
. If you can't send the object
in body
of request
then you don't need these line:
db.Products.Attach(dbProduct);
var entry = db.Entry(dbProduct);
entry.Property(e => e.Description).IsModified = true;
Upvotes: 1
Reputation: 53
As you are working in Connected mode of Entity framework there is no need to use Attach method, in this default mode when you get an object from database Entity framework will store the state of object and when you call method SaveChanges() Entity framework will compare the state of object and apply the changes. We use Attach when we are working in disconnect mode. In that case we need to give the state of object to Entity framework by using Attach.
Upvotes: 0
Reputation: 960
Try this Code :-
var dbProduct = db.Products.FirstOrDefault(s => s.InternalReferenceId == InternalReferenceId && s.SupplierId == SupplierId);
if (dbProduct != null)
{
dbProduct.Description="Bla Bla Bla ";
db.Products.Attach(dbProduct);
var entry = db.Entry(dbProduct);
entry.Property(e => e.Description).IsModified = true;
await db.SaveChangesAsync();
}
else
{
return NotFound();
}
return Ok();
Upvotes: 0