Error:A referential integrity constraint violation occurred on db.SaveChanges() in .net?(错误:.net 中的 db.SaveChanges() 发生参照完整性约束冲突?)
问题描述
我使用 Entity framework 4.0. 创建了一个 WPF 应用程序.当我尝试在 PhoneNumber 表中插入记录时,它会成功插入第一条记录.但是,当我遍历一些列表并尝试将另一个项目插入 PhoneNumber 表时,它插入记录但显示错误为:
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 由用户代码处理:对数据库的更改已成功提交,但在更新对象上下文时出错.ObjectContext 可能处于不一致的状态.内部异常消息:发生引用完整性约束冲突:定义引用约束的属性值在关系中的主体对象和依赖对象之间不一致.
.cs 文件中的代码:
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();
                    }
                }
            }
}
在 PhoneNumber 表中插入第二条记录后出现错误.有什么想法吗? 
Getting Error after inserting Second record in PhoneNumber table. Any idea? 
表结构:
帮助赞赏!
推荐答案
您只实例化一次 phnumber,然后尝试多次将其插入数据库.在 foreach (var ph in phoneList) 中移动 PhoneNumber phnumber = new PhoneNumber(); 短语阻止.
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.
这篇关于错误:.net 中的 db.SaveChanges() 发生参照完整性约束冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:.net 中的 db.SaveChanges() 发生参照完整性约束冲突?
				
        
 
            
        - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 
						
						
						
						
						
				
				
				
				