1

I have a project I am using a database that is connected thru the entity data model and I am using the DBcontext to create and update entities.

My Create method (below) is working fine.

[HttpPost]
public IHttpActionResult PostCustomer([FromBody] Customer Customer)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    using (var dbCtx = new CustomerDBEntities())
    {
        dbCtx.Customers.Add(Customer);
        dbCtx.SaveChanges();

    }
    return CreatedAtRoute("DefaultApi", new { id = Customer.id }, Customer);
}

But my update method is doing nothing. I am not getting an error or anything just nothing seems to happen. and it is not updating the values. The code is this

[HttpPut]
public IHttpActionResult UpdateCustomer([FromBody] Customer Customer)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    Customer cust;
    var ctx = new CustomerDBEntities();
    cust = ctx.Customers.Where(s => s.id == Customer.id).FirstOrDefault<Customer>();
    if (cust != null)
    {
        cust.Customer_Name = Customer.Customer_Name;
        cust.email = Customer.email;
        cust.Customer_address = Customer.Customer_address;

    }

    ctx.Entry(cust).State = System.Data.EntityState.Modified;
    ctx.SaveChanges();


    return CreatedAtRoute("DefaultApi", new { id = Customer.id }, Customer);
}

When I was using an SQL command before to go directly to database it was working fine but ever since I changed it it stopped working.

Any help would be much appreciated.

Yuval Haran
  • 119
  • 1
  • 13
  • if (cust != null) <- are you sure this is not null? – Daniel Feb 01 '17 at 23:14
  • Possible duplicate of [How to update record using Entity Framework 6?](http://stackoverflow.com/questions/25894587/how-to-update-record-using-entity-framework-6) – Craig W. Feb 01 '17 at 23:15
  • You dont ideally need to manually set (`ctx.Entry(cust).State = System.Data.EntityState.Modified;`) the entity state as the entity is being fetched in the same context- EF change tracker will do the work for you. However, setting the state should not affect anything though. Put a try catch and verify there are no errors. Also for performance sake, you can get the entity like `ctx.Customers.Find (Customer.id)` instead oh `Where` and `FirstOrDefault` – Developer Feb 01 '17 at 23:17
  • There's nothing obviously wrong with your code (apart from that it's updating too much, as commented). Are you sure you're checking the right database? Also, monitor the SQL that is executed. – Gert Arnold Feb 01 '17 at 23:20
  • Hi all thanks for your responses it works partialy for me now now it works when all of the fields are entered but doesnt work when the email field is unchanged. if the name or address are unchanged it works it only fails when the email field is unchanged any suggestions? – Yuval Haran Feb 01 '17 at 23:24
  • Is `email` just a string? – Gert Arnold Feb 02 '17 at 09:21

2 Answers2

2

Remove this line of code.

ctx.Entry(cust).State = System.Data.EntityState.Modified;

EF tracks the changes made to entities in the context, you do not need to tell it that it was modified.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • Hi thanks for the help it worked for me, but now i have another problem it works when all of the fields are changed or when the email field is changed but when the email is unchanged it still fails, if name or address are unchanged it still works only email field is giving me problems now – Yuval Haran Feb 01 '17 at 23:27
  • You should ask a new question rather than editing the existing one and rendering all the comments and answers out-of-context. – Craig W. Feb 02 '17 at 15:26
1

Add changes at Property Level

context.Entry(Customer).Property(cust => cust.Customer_Name).IsModified = true; 

More Details here https://msdn.microsoft.com/en-us/data/jj592677

Also similar question answered here https://stackoverflow.com/a/15339512/1481690

Community
  • 1
  • 1
Peru
  • 2,871
  • 5
  • 37
  • 66