ASP.NET MVC and Entity Framework. How to avoid duplicate rows?
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 开发者_如何学Go | 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.
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);
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.
精彩评论