2014-12-28 77 views
1

我JSON数据像这样的:存储JSON数据对象在ASP.NET MVC(实体框架)数据库

{ 
    "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" 
} 

我想存储使用ASP.NET MVC实体框架这个JSON数据到数据库(使用Microsoft SQL数据库)。我的模型是使用ADO.NET实体框架数据模型从数据库自动生成的。我面临的问题是,由于某种原因,它不会将电子邮件/电话和标签存储在数据库中。我尝试了所有我能找到的东西,而他们都没有为我工作。有人知道问题在哪里,我该如何将这个JSON存储到我的数据库?

编辑1:

后,我调试控制器接触可变我注意到,电子邮件,标签和手机型号的数据是空的。我不知道他们是如何以及为什么是空的......

我也加约束的外键

这里是数据库模型:

Database model

这是自动生成的模型从数据库(EF数据模型)

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

,这里是模型电话/标签/电子邮件表(它同样使用不同的名称在一列)

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

这里是数据添加到数据库功能:

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

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

你遇到的错误是什么? – Ofiris

+0

@Ofiris没有错误...来自JSON对象的电话,标签和电子邮件数据不会存储到数据库中... – jureispro

+0

如果将这些“HashSet ”收藏集更改为“列表”,该怎么办? – dbc

回答

3

你的JSON应该是这个样子:

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

和我有过它这样:

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

      } 
     }); 

然后下载JSON.Net

,做这样的事情:

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

的“身份证”不应该是空的,因为通过DeserializeObject你会得到一个异常。

我希望它能帮助你。

相关问题