0

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.

2
  • Can you post your entire method that you're using to update the cleaner value? What's probably happening, is that you're attaching your new cleaner object when the job object is not attached to a context. Because of that, the FK is not being updated since they're not context to manage it, and when you do attach it to a context, since it's not correct you get the error. You can either attach it to the context prior to making the update, or update your FK value. Commented Nov 14, 2012 at 14:40
  • There is an MVC page which uses ajax and knockout to retrieve the job object and the cleaner int value is changed and PUT to the save method above. Is there a way to update the object before this save code manually for the FK. Commented Nov 14, 2012 at 15:09

1 Answer 1

2

This exception usually occurs if job.ValeterInfo != null and job.ValeterInfo.ValeterId != job.Cleaner. So, the simplest solution is to set the navigation property to null before you attach the job to the context:

if (ModelState.IsValid)
{
    job.ValeterInfo = null;
    db.Entry(job).State = EntityState.Modified;
    db.SaveChanges();
}

This looks a bit strange and like a hack. But the question is why job.ValeterInfo is NOT null when you post the data to the controller action. When you set the state of the job to Modified you are only updating the job's scalar properties (including Cleaner) but not any properties of job.ValeterInfo or any relationships. So, you don't need to send job.ValeterInfo properties to the server in the first place.

Anyway, you have an inconsistency: The FK job.Cleaner is changed but the related entity job.ValeterInfo (especially its primary key property ValeterId) is not. EF doesn't know which represents the correct relationship: The foreign key property value or the navigation property value? This ambiguity causes the exception.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.