ASP.NET MVC 和实体框架。如何避免重复行?
所以我是 ASP.NET MVC 的新手,我正在尝试实现一个地址管理器。我正在使用 Linq2Entity Framework。我有一张包含联系人的表格和一张包含电话号码的表格。这两个表通过 contact_has_phone_number 表链接。实体框架使我能够获取分配给一位联系人的所有电话号码。这是通过调用 contact.PhoneNumbers 来完成的,它返回电话号码的集合。使用 contact.PhoneNumber.Add(number) 添加新号码。问题是我在电话号码表中收到重复的电话号码条目。我想要得到的是现有电话号码仅在 contact_has_phone_number 表中链接。 例如:
表“contact”
ID_Contact | First_Name | Last_Name
1 | Jeff | Bridges
2 | Peter | Miller
表“contact_has_phone_number”
ID_Contact | ID_Number
1 | 1
1 | 2
2 | 1
表“phone_number”
ID_Number | Number
1 | 1234567
2 | 7654321
在 momonet 中,我会在“phone_number”表中得到 3 行,其中 2 行具有相同的号码。
ID_Number | Number
1 | 1234567
2 | 7654321
3 | 1234567
如果你能帮我解决这个问题,那就太棒了。
So I'm new to ASP.NET MVC and I'm trying to implement an address manager.I'm using Linq2Entity Framework. I've got a table with contacts and one with phone numbers. These two tables are linked by a contact_has_phone_number table. The Entity Framework enables me to get all phone numbers that are assigned to one contact. This is done by calling contact.PhoneNumbers which returns a collection of phone numbers. A new number is added with contact.PhoneNumber.Add(number). The Problem is that I am getting duplicate phone number entries in the phone number table. What I would like to get is that an existing phone number only gets linked in the contact_has_phone_number table.
For Example:
Table "contact"
ID_Contact | First_Name | Last_Name
1 | Jeff | Bridges
2 | Peter | Miller
Table "contact_has_phone_number"
ID_Contact | ID_Number
1 | 1
1 | 2
2 | 1
Table "phone_number"
ID_Number | Number
1 | 1234567
2 | 7654321
At the momonet I would get 3 rows in the "phone_number" table with 2 rows with identical numbers.
ID_Number | Number
1 | 1234567
2 | 7654321
3 | 1234567
Would be a blast if you could help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是多对多的情况。我遇到过这样的情况(与联系无关),这就是我所做的。
在创建新联系人时,我将获得联系方式和电话号码。可以使用下面的代码
编辑:
我应该在方法的开头、循环之前完成此操作。
然后,行
contact.PhoneNumbers.Add(storedPhone);
将变为
newContact.PhoneNumbers.Add(storedPhone);
This is a many to many situation. I have encountered a situation like this (not contact related) and this is what I did.
In creating a new contact, I will have the contact details as well as the phone numbers. the below code can be used
EDIT:
I should have done this at the beginning of the method, before the loop.
Then, the lines
contact.PhoneNumbers.Add(storedPhone);
will become
newContact.PhoneNumbers.Add(storedPhone);
我在使用 EF 时遇到了非常类似的重复记录问题。经过一番混乱后,我终于找到了解决方案: 在
执行 .SaveChanges() 之前,您必须始终将新创建的实体附加到表中
我的问题是,在将我创建的新实体附加到表中之前调用了 SaveChanges,因此创建了一个实体调用 SaveChanges 并在 AddObject();SaveChanges(); 后调用另一个重复实体后被运行。
TL;DR:
具有重复记录的结果:
已更改为:
注意:数据库名称已更改以保护特权客户端。
I had a very similar problem with duplicate records occurring using EF. After much mess, I finally discovered the solution:
You must always attach newly created entities to a table before executing .SaveChanges()
My issue was that SaveChanges was being called before attaching a new entity I created to a table so there was one entity created after SaveChanges was called and another duplicate entity called after AddObject();SaveChanges(); was run.
TL;DR:
Result with duplicate records:
was changed to:
Note: database names changed to protect privileged client.