title: Store JSON data object to database in ASP.NET MVC (Entity Framework)
body: <p>I have JSON data like this one:</p>
  
  <pre><code>{
     "id":"",
     "firstName":"John",
     "lastName":"Doe",
     "phones":[
        {
           "value":"555-555-555",
           "$$hashKey":"object:8"
        },
        {
           "value":"444-444-444",
           "$$hashKey":"object:10"
        }
     ],
     "emails":[
        {
           "value":"example@mail.com",
           "$$hashKey":"object:12"
        },
        {
           "value":"othermail@mail.com",
           "$$hashKey":"object:18"
        }
     ],
     "tags":[
        {
           "value":"friend",
           "$$hashKey":"object:14"
        },
        {
           "value":"john",
           "$$hashKey":"object:16"
        }
     ],
     "address":"Route 44",
     "city":"NY",
     "bookmarked":"",
     "notes":"It's John"
  }
  </code></pre>
  
  <p>I want to store this JSON data to database using ASP.NET MVC entity framework (Using Microsoft SQL Database). My model is auto-generated from database using ADO.NET entity framework data model. Problem that I faced is that for some reason it won't store emails/phones and tags in database. I tried everything that I could find and none of them worked for me. Does someone knows where's the problem and how can I store this JSON to my database?</p>
  
  <p><strong><em>EDIT 1:</em></strong></p>
  
  <p>After I debug <em>contact</em> variable in controller I notice that Email, Tags and Phones Model data are empty. I don't know how and why they are empty...</p>
  
  <p>Also I add CONSTRAINT to all foreign keys </p>
  
  <p>Here is database model:</p>
  
  <p><img src="https://i.stack.imgur.com/fV6bZ.png" alt="Database model"></p>
  
  <p>this is auto-generated model from database (EF data model)</p>
  
  <pre><code> public partial class Contact
      {
          public Contact()
          {
              this.Emails1 = new HashSet&lt;Email&gt;();
              this.Phones1 = new HashSet&lt;Phone&gt;();
              this.Tags1 = new HashSet&lt;Tag&gt;();
          }
  
          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&lt;byte&gt; bookmarked { get; set; }
          public string notes { get; set; }
  
          public virtual ICollection&lt;Email&gt; Emails1 { get; set; }
          public virtual ICollection&lt;Phone&gt; Phones1 { get; set; }
          public virtual ICollection&lt;Tag&gt; Tags1 { get; set; }
      }
  }
  </code></pre>
  
  <p>and here is model For Phone/Tags/Email tables (it's same with different name in one column)</p>
  
  <pre><code>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; }
      }
  </code></pre>
  
  <p>and here is function that add data to database:</p>
  
  <pre><code> public string AddContact(Contact contact)
          {
              if (contact != null)
              {
  
                  db.Contacts.Add(contact);
                  db.SaveChanges();
                      return "Contact Added";
              }
              else
              {
                  return "Invalid Record";
              }
          }
  </code></pre>