I have the following model:
public class Job
{
[Key]
public int JobID { get; set; }
public string Status { get; set; }
public DateTime JobDate { get; set; }
public string JobTitle { get; set; }
public int? Cleaner { get; set; }
public int? Client { get; set; }
public int EstTime { get; set; }
public virtual Client ClientInfo { get; set; }
public virtual Valeter ValeterInfo { get; set; }
}
This in OnModelCreating:
// Relationship Job -> Valeter
modelBuilder.Entity<Job>()
.HasOptional<Valeter>(u => u.ValeterInfo)
.WithMany()
.HasForeignKey(e => e.Cleaner);
(NOTE: it is using an existing database). When I try to perform the following:
if (ModelState.IsValid)
{
db.Entry(job).State = EntityState.Modified;
db.SaveChanges();
}
It generally works fine UNLESS I change the Cleaner value to something else and then I get the error:
A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.