I have a model, 'Person', that references another model, 'Salutation'.
public class Person
{
public int Id { get; set; }
public Boolean Active { get; set; }
// fk
public virtual Salutation Salutation { get; set; }
public virtual PersonName Name { get; set; }
}
public class Salutation
{
public int Id { get; set; }
public string Name { get; set; }
}
when I try and UPDATE the 'Person' with a different 'Salutation' it doesn't update. Though if I change the actual data within the 'salutation' it does update. This is the UPDATE code in my controller, the data coming in to the function is correct but just doesn't save in the DB.
For example if the current Salutation has ID: 1 and Name: "Mr" then if I try and pass in a different existing record with ID: 2 and Name: "Mrs" it doesn't change. But if I pass in ID:2 and Name:"RandomAlienString" then it DOES change to the new model and updates the Salutation.
In the controller - UPDATE Method:
public void PutPerson(int id, [FromBody]Person person)
{
if (!ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
if (id != person.Id)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
db.Entry(person).State = EntityState.Modified;
db.Entry(person.Name).State = EntityState.Modified;
db.Entry(person.Salutation).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!PersonExists(id))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
else
{
throw;
}
}
}
Any help would be most appreciated.