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:
后,我调试控制器接触可变我注意到,电子邮件,标签和手机型号的数据是空的。我不知道他们是如何以及为什么是空的......
我也加约束的外键
这里是数据库模型:
这是自动生成的模型从数据库(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";
}
}
你遇到的错误是什么? – Ofiris
@Ofiris没有错误...来自JSON对象的电话,标签和电子邮件数据不会存储到数据库中... – jureispro
如果将这些“HashSet”收藏集更改为“列表”,该怎么办? –
dbc