title: How to update current entities using entity framework
body: <p>I have a gmail service that returns me the names and phonenumbers of my contacts. to store these information into my database I have created two simple poco classes</p>
  
  <p>so what I want to do is : 1-to prevent duplicates 
  2- I may update contacts information on the gmail so next time I call the below function I would like to update the contacts. how do I do this?</p>
  
  <pre><code>public class CONTACTS_INFO
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int CI_ROWID { get; set; }
  
      public int CI_CONTACT_REFNO { get; set; }
  
      public string CI_PHONE_NUMBERS { get; set; }
  }
  
  public class GMAIL_CONTACTS
      {
          public GMAIL_CONTACTS()
          {
              CONTACTS_INFO = new List&lt;CONTACTS_INFO&gt;();
          }
          [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int GC_ROWID { get; set; }
  
          public string GC_CONTACT_NAME { get; set; }
  
          [ForeignKey("CI_CONTACT_REFNO")]
          public List&lt;CONTACTS_INFO&gt; CONTACTS_INFO { get; set; }
      }
  </code></pre>
  
  <p>following is my method </p>
  
  <pre><code>using (var context = new MPContext())
              {
  
                  foreach (var contact in cr.GetContacts().Entries)
                  {
                      if (string.IsNullOrEmpty(contact.Name.FullName)) continue;
                      var gContacts = new GMAIL_CONTACTS { GC_CONTACT_NAME = contact.Name.FullName };
  
                      foreach (var phonenumber in contact.Phonenumbers)
                      {
                          if (string.IsNullOrEmpty(phonenumber.Label)) continue;
                          gContacts.CONTACTS_INFO.Add(new CONTACTS_INFO { CI_PHONE_NUMBERS = phonenumber.Value + phonenumber.Label });
                      }
                     //Here I would like to do two things
                     //1-Check if I have this contact in database so I wont save it
                     //2-I may change information on the gmail so I would like to update my contacts 
                          context.GMAIL_CONTACTS.Add(gContacts);                       
                  }
                  context.SaveChanges();
              }      
  </code></pre>