jureispro
jureispro

Reputation: 1402

Store JSON data object to database in ASP.NET MVC (Entity Framework)

I have JSON data like this one:

{
   "id":"",
   "firstName":"John",
   "lastName":"Doe",
   "phones":[
      {
         "value":"555-555-555",
         "$$hashKey":"object:8"
      },
      {
         "value":"444-444-444",
         "$$hashKey":"object:10"
      }
   ],
   "emails":[
      {
         "value":"[email protected]",
         "$$hashKey":"object:12"
      },
      {
         "value":"[email protected]",
         "$$hashKey":"object:18"
      }
   ],
   "tags":[
      {
         "value":"friend",
         "$$hashKey":"object:14"
      },
      {
         "value":"john",
         "$$hashKey":"object:16"
      }
   ],
   "address":"Route 44",
   "city":"NY",
   "bookmarked":"",
   "notes":"It's John"
}

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?

EDIT 1:

After I debug contact variable in controller I notice that Email, Tags and Phones Model data are empty. I don't know how and why they are empty...

Also I add CONSTRAINT to all foreign keys

Here is database model:

Database model

this is auto-generated model from database (EF data model)

 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; }
    }
}

and here is model For Phone/Tags/Email tables (it's same with different name in one column)

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; }
    }

and here is function that add data to database:

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

                db.Contacts.Add(contact);
                db.SaveChanges();
                    return "Contact Added";
            }
            else
            {
                return "Invalid Record";
            }
        }

Upvotes: 2

Views: 18127

Answers (1)

Aria
Aria

Reputation: 3844

Your JSON should be look like this:

 var json = {
            "id": "123",
            "firstName": "John",
            "lastName": "Doe",
            "phones": [
               {
                   "number": "555-555-555"
               },
               {
                   "number": "444-444-444"
               }
            ],
            "emails": [
               {
                   "email1": "[email protected]"
               },
               {
                   "email1": "[email protected]"
               }
            ],
            "tags": [
               {
                   "tag1": "friend"
               },
               {
                   "tag1": "john"
               }
            ],
            "address": "Route 44",
            "city": "NY",
            "bookmarked": "",
            "notes": "It's John"
        };

And I have pass it in this way:

$.ajax({
            url: 'YourAddress',
            data: { Action: 'CheckJson', JSONData: JSON.stringify(json) },
            dataType: "json",
            async: false,
            success: function (data) { 
            },
            error: function (x, e) {

            }
        });

Then Download JSON.Net

And do something like this :

string json = request["JSONData"];
Contact m = JsonConvert.DeserializeObject<Contact>(json);

The 'id' should not be empty because by DeserializeObject you will get an exception.

I hope it will help you.

Upvotes: 4

Related Questions