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

▼魔方 西西 提交于 2019-12-01 09:43:48

问题


I have a created a WPF application with Entity framework 4.0. When i am trying to insert record in PhoneNumber Table it inserts first record successfully. But, when i loop through some List and try to insert another item into PhoneNumber table it Insert the record but shows error as:

InvalidOperationException was handled by user code: The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: 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.

Code in .cs file:

protected void InsertContact(List<PhoneNumbersList> phoneList,otherparameters here)
{
             Contact contact = new Contact();
             PhoneNumber phnumber = new PhoneNumber();

            //INSERT into Contacts Table
            contact.PrefixTitleID = cfnm.PrefixTitleID;// cfnm.Title;
            contact.FirstName = cfnm.FirstName;
            contact.MiddleName = cfnm.MiddleName;
            contact.LastName = cfnm.LastName;
            contact.Website = webpageaddress;
            contact.SuffixID = cfnm.SuffixID;
            contact.Gender = gender;
            contact.IMAddress = imaddress;

            db.Contacts.Add(contact);
            db.SaveChanges();
            int contactid=contact.ContactID;
            if (contactid > 0)
            {
                int phid = 0;
                //INSERT into PhoneNumber Table
                foreach (var ph in phoneList)
                {

                    phnumber.PhoneTypeID = ph.PhoneTypeID;
                    phnumber.CountryID = ph.CountryID;
                    phnumber.City = ph.City;
                    phnumber.LocalNumber = ph.LocalNumber;
                    phnumber.Extension = ph.Extension;
                    phnumber.CountryCode = ph.CountryCode;
                    db.PhoneNumbers.Add(phnumber);
                    db.SaveChanges();//Getting Error here
                    phid=phnumber.ID ;
                    if(phid > 0)
                    {
                        //Save in ContactPhoneNumber Table
                        contactphonenumber.ContactID = contactid;
                        contactphonenumber.PhoneNumberID = phid;
                        db.ContactPhoneNumbers.Add(contactphonenumber);
                        db.SaveChanges();
                    }
                }
            }
}

Getting Error after inserting Second record in PhoneNumber table. Any idea?

Table Structure:

Help Appreciated!


回答1:


You instantiate phnumber only once and then try to insert it in the database multiple times. Move the PhoneNumber phnumber = new PhoneNumber(); phrase inside the foreach (var ph in phoneList) Block.



来源:https://stackoverflow.com/questions/17502470/errora-referential-integrity-constraint-violation-occurred-on-db-savechanges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!