[RESOLVED]Entity Framework Edit Model

Hello everybody

I just start work with Entity Framework and when I try to edit model I got a 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."

The model what I try to edit is:

    public class CashboxCharge
    {
        [HiddenInput(DisplayValue = false)]
        public virtual Int32 ID { get; set; }

        //[DataType(DataType.Date)]
        //[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
        public virtual DateTime Date { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual Int32 OrganizationID { get; set; }

        [ForeignKey("OrganizationID")]
        public virtual Organization Organization { get; set; }

        public virtual IList<CashboxChargeItem> CashboxChargeItems { get; set; }
    }

and 

public class CashboxChargeItem
    {
        [HiddenInput(DisplayValue = false)]
        public virtual Int32 ID { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual Int32 ChargeID { get; set; }

        public virtual Charge Charge { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual Int32 ProgramID { get; set; }

        public virtual Program Program { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual Int32 SpecificityID { get; set; }

        public virtual Specificity Specificity { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual Int32 ArticleExpendureID { get; set; }

        public virtual ArticleExpendure ArticleExpendure { get; set; }

        public virtual float ExSumma { get; set; }
        public virtual float Summa { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual Int32 CashboxChargeID { get; set; }

        [ForeignKey("CashboxChargeID")]
        public virtual CashboxCharge CashboxCharge { get; set; }
    }

For Edit I use code which was created by Add Controller

[HttpPost]
        public ActionResult Edit(CashboxCharge cashboxcharge, String param)
        {
            if (ModelState.IsValid)
            {
                cashboxcharge.CashboxChargeItems = JSON.Deserialize<IList<CashboxChargeItem>>(param);

                db.Entry(cashboxcharge).State = EntityState.Modified;
                db.SaveChanges();               
            }
            return this.Direct();
        }

Hi VSirchenko,

Thanks for your post.

VSirchenko

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."

Its seems that in some of its  collections that set the relationship with other entities exist some type of conflict. You could try to clear all the collections to see if the problem persists,  in this case you lost the foreign key assignment. But perhaps
could help you to locate the problem.

More information:

Entity Framework: A referential integrity constraint violation on many to many relationship

A referential integrity constraint violation occurred

Error:A referential integrity constraint violation occurred on db.SaveChanges() in .net?

If there’s anything else I can do for you regarding this matter,please feel free to let me know.

Best Regards,

Eileen

Leave a Reply