ASP.NET MVC 和实体框架。如何避免重复行?

发布于 2024-10-20 12:07:43 字数 956 浏览 1 评论 0原文

所以我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-10-27 12:07:43

这是多对多的情况。我遇到过这样的情况(与联系无关),这就是我所做的。

在创建新联系人时,我将获得联系方式和电话号码。可以使用下面的代码

public void AddNewContact(Contact contact)
{
    var storedPhones = context.PhoneNumbers.Select(s => s).ToList();
            foreach (var s in contact.PhoneList)
            {
                var p = s.PhoneNumber; 
                var storedPhone = storedPhone s.Where(f => f.PhoneNumber == p).Select(t => t).FirstOrDefault();
                if (storedPhone == null)
                {
                    var newPhoneNumber = MySqlEFModel.PhoneNumber.CreatePhoneNumber(Guid.NewGuid(), p);
                    context.AddToPhoneNumbers(newPhone);
                    contact.PhoneNumbers.Add(newPhone);
                }
                else
                {
                    contact.PhoneNumbers.Add(storedPhone);
                }
            }
        context.AddToContacts(contact);
        context.SaveChanges();
}

编辑:

我应该在方法的开头、循环之前完成此操作。

                    var newContact = MySqlEFModel.Contact.CreateContact(keyfields, ... ... , ...);

然后,行

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

public void AddNewContact(Contact contact)
{
    var storedPhones = context.PhoneNumbers.Select(s => s).ToList();
            foreach (var s in contact.PhoneList)
            {
                var p = s.PhoneNumber; 
                var storedPhone = storedPhone s.Where(f => f.PhoneNumber == p).Select(t => t).FirstOrDefault();
                if (storedPhone == null)
                {
                    var newPhoneNumber = MySqlEFModel.PhoneNumber.CreatePhoneNumber(Guid.NewGuid(), p);
                    context.AddToPhoneNumbers(newPhone);
                    contact.PhoneNumbers.Add(newPhone);
                }
                else
                {
                    contact.PhoneNumbers.Add(storedPhone);
                }
            }
        context.AddToContacts(contact);
        context.SaveChanges();
}

EDIT:

I should have done this at the beginning of the method, before the loop.

                    var newContact = MySqlEFModel.Contact.CreateContact(keyfields, ... ... , ...);

Then, the lines

contact.PhoneNumbers.Add(storedPhone);

will become

newContact.PhoneNumbers.Add(storedPhone);

星星的軌跡 2024-10-27 12:07:43

我在使用 EF 时遇到了非常类似的重复记录问题。经过一番混乱后,我终于找到了解决方案: 在

执行 .SaveChanges() 之前,您必须始终将新创建的实体附加到表中

我的问题是,在将我创建的新实体附加到表中之前调用了 SaveChanges,因此创建了一个实体调用 SaveChanges 并在 AddObject();SaveChanges(); 后调用另一个重复实体后被运行。

TL;DR:

具有重复记录的结果:

Pro.Machine = new XXXDB.Machine();
Pro.Machine.Translate(machine); // SaveChanges() was being called by this method.
XXXDB.XXXDB.DB.Machines.AddObject(Pro.Machine);
XXXDB.XXXDB.DB.SaveChanges();

已更改为:

Pro.Machine = new XXXDB.Machine();
XXXDB.XXXDB.DB.Machines.AddObject(Pro.Machine);
Pro.Machine.Translate(machine); //SaveChanges() occurs after attaching to the machines table.
XXXDB.XXXDB.DB.SaveChanges();

注意:数据库名称已更改以保护特权客户端。

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:

Pro.Machine = new XXXDB.Machine();
Pro.Machine.Translate(machine); // SaveChanges() was being called by this method.
XXXDB.XXXDB.DB.Machines.AddObject(Pro.Machine);
XXXDB.XXXDB.DB.SaveChanges();

was changed to:

Pro.Machine = new XXXDB.Machine();
XXXDB.XXXDB.DB.Machines.AddObject(Pro.Machine);
Pro.Machine.Translate(machine); //SaveChanges() occurs after attaching to the machines table.
XXXDB.XXXDB.DB.SaveChanges();

Note: database names changed to protect privileged client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文