Skip to main content
 首页 » 编程设计

c#之将 JSON 数据对象存储到 ASP.NET MVC( Entity Framework )中的数据库

2024年08月06日27me-sa

我有这样的 JSON 数据:

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

我想使用 ASP.NET MVC Entity Framework (使用 Microsoft SQL 数据库)将此 JSON 数据存储到数据库中。我的模型是使用 ADO.NET Entity Framework 数据模型从数据库自动生成的。我遇到的问题是,出于某种原因,它不会在数据库中存储电子邮件/电话和标签。我尝试了所有我能找到的东西,但没有一个对我有用。有人知道问题出在哪里以及如何将此 JSON 存储到我的数据库中吗?

编辑 1:

在 Controller 中调试contact 变量后,我注意到电子邮件、标签和电话模型数据为空。我不知道它们是如何以及为什么是空的...

我还为所有外键添加了 CONSTRAINT

这是数据库模型:

这是从数据库(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; } 
    } 
} 

这里是 Phone/Tags/Email 表的模型(它与一列中的不同名称相同)

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

请您参考如下方法:

您的 JSON 应如下所示:

 var json = { 
            "id": "123", 
            "firstName": "John", 
            "lastName": "Doe", 
            "phones": [ 
               { 
                   "number": "555-555-555" 
               }, 
               { 
                   "number": "444-444-444" 
               } 
            ], 
            "emails": [ 
               { 
                   "email1": "example@mail.com" 
               }, 
               { 
                   "email1": "othermail@mail.com" 
               } 
            ], 
            "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); 

'id' 不应该为空,因为通过 DeserializeObject 你会得到一个异常。

希望对您有所帮助。