Entity Framework inserts new rows instead of updating them

I have a problem updating data in the database. When I want to update the data, Entitiy Framework adds new rows to tables that can have multiple rows (tables with a foreign key).

Database model:

Database model

When I update the Phone / Contact or Tags object, the Entity Framework automatically adds a new row instead of updating.

Here is the code I used:

public string UpdateContact(Contact contact)
{
    if (contact != null)
    {

        int id = Convert.ToInt32(contact.id);
        Contact Updatecontact = db.Contacts.Where(a => a.id == id).FirstOrDefault();
        Updatecontact.firstname = contact.firstname;
        Updatecontact.lastname = contact.lastname;
        Updatecontact.address = contact.address;
        Updatecontact.bookmarked = contact.bookmarked;
        Updatecontact.city = contact.city;
        Updatecontact.notes = contact.notes;
        Updatecontact.Emails1 = contact.Emails1;
        Updatecontact.Phones1 = contact.Phones1;
        Updatecontact.Tags1 = contact.Tags1;
        db.SaveChanges();
        return "Contact Updated";

    }
    else
    {
        return "Invalid Record";
    }
}

      

EDIT:

Here is the EF model code:

Contact:

public partial class Contact
{
    public Contact()
    {
        this.Emails1 = new HashSet<Email>();
        this.Phones1 = new HashSet<Phone>();
        this.Tags1 = new HashSet<Tag>();
    }

    public int id { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public Nullable<byte> bookmarked { get; set; }
    public string notes { get; set; }

    public virtual ICollection<Email> Emails1 { get; set; }
    public virtual ICollection<Phone> Phones1 { get; set; }
    public virtual ICollection<Tag> Tags1 { get; set; }
}

      

Letters / tags and phone are the same model (with a different name for meaning)

public partial class Email
{
        public int id { get; set; }
        public int id_contact { get; set; }
        public string email1 { get; set; }

        public virtual Contact Contact1 { get; set; }
}

      

+3


source to share


3 answers


Update properties, not set new objects.

  Updatecontact.Emails1.email1 = contact.Emails1.email1;
  Updatecontact.Phones1.number = contact.Phones1.number;
  Updatecontact.Tags1.tag1 = contact.Tags1.tag1;

      



Edit: it seems like your contact model has lists of letters, phone numbers and tags. If so, then a simple assignment will not work. Instead, when submitting from the client, you have to find one by one and update:

 foreach ( var email in contact.Emails1 )
 {
     // first make sure the object is retrieved from the database 
     var updateemail = Updatecontact.Emails1.FirstOrDefault( e => e.id == email.id );
     // then update its properties
     updateemail.email1 = email.email1;
 }

 // do the same for phones and tags

      

+5


source


This is because you are setting different values HashSet

for the values โ€‹โ€‹of a completely different collection, namely from what you call contact

in that method. In order for you to be able to update correctly, you will have to loop through the emails, phones and tags to check if they need to be added / updated / removed on the actual object you are trying to update.



+1


source


First, why do you need to search for a contact if you are already getting it by parameter? This makes it think that you are creating a new one because you are in a different context, if so then it creates a new entry because you have 2 different objects in 2 different contexts.

Try to use only one object in the same context for updating, EF has to mark the object for modification itself, if not try before saving that your object has EntityState.Modified.

+1


source







All Articles