Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON字符串数组转换为实体框架_Json_Entity Framework_Json.net - Fatal编程技术网

将JSON字符串数组转换为实体框架

将JSON字符串数组转换为实体框架,json,entity-framework,json.net,Json,Entity Framework,Json.net,我正在尝试使用自动标识键和正确的外键关系将JSON文件放入SQL Server数据库。除了字符串数组之外,所有的东西都在出色地工作。数据大致如下所示: { "id" : "123", "name" : "Some Stuff", "phrase" : "More Stuff", "type" : "ABC", "label" : "Some label", "responseType" : "The Response Type", "answers" : [ "9"

我正在尝试使用自动标识键和正确的外键关系将JSON文件放入SQL Server数据库。除了字符串数组之外,所有的东西都在出色地工作。数据大致如下所示:

{
  "id" : "123",
  "name" : "Some Stuff",
  "phrase" : "More Stuff",
  "type" : "ABC",
  "label" : "Some label",
  "responseType" : "The Response Type",
  "answers" : [ "9" ]
}
“答案”部分让我感到不适。看起来它几乎完全是一个值,但它可能有多个值,比如

"answers" : [ "6", "7", "8" ]
现在我知道EF中不支持字符串或int等本机类型的列表。我最终还是希望有一个单独的表来显示应答值列表,我称之为DataAnswers

        public partial class Response
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ResponseId { get; set; }

        [JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
        public string Id { get; set; }

        [JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
        public string Name { get; set; }

        [JsonProperty("phrase", NullValueHandling = NullValueHandling.Ignore)]
        public string Phrase { get; set; }

        [JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
        public string Type { get; set; }

        [JsonProperty("label", NullValueHandling = NullValueHandling.Ignore)]
        public string Label { get; set; }

        [JsonProperty("responseType", NullValueHandling = NullValueHandling.Ignore)]
        public string ResponseType { get; set; }

        [JsonProperty("answers", NullValueHandling = NullValueHandling.Ignore)]
        public virtual List<DataAnswer> DataAnswers { get; set; }
    }

    public partial class DataAnswer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DataAnswerId { get; set; }
        public string AnswerText { get; set; }
    }
公共部分类响应
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int响应ID{get;set;}
[JsonProperty(“id”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串Id{get;set;}
[JsonProperty(“名称”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串名称{get;set;}
[JsonProperty(“短语”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串短语{get;set;}
[JsonProperty(“类型”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串类型{get;set;}
[JsonProperty(“标签”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串标签{get;set;}
[JsonProperty(“responseType”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串响应类型{get;set;}
[JsonProperty(“answers”,NullValueHandling=NullValueHandling.Ignore)]
公共虚拟列表数据应答{get;set;}
}
公共部分类数据应答
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
public int DataAnswerId{get;set;}
公共字符串应答文本{get;set;}
}
目前,我得到了一个错误

Newtonsoft.Json.JsonSerializationException:'转换值时出错 “9”键入“demo.Data.DataAnswer”。路径 “项目[0]。响应[0]。答案[0]”,第60行,位置23。”

关于如何将答案字符串列表放入带有外键的表中,有什么好主意吗


提前谢谢

您可以创建一个数据传输对象,然后处理该对象与实体对象之间的转换

您可以创建一个用于处理属性转换的

您可以创建用于序列化/反序列化但不由EF使用的附加属性,并在其中处理转换:

[JsonIgnore]
public virtual List<DataAnswer> DataAnswers { get; set; }
[NotMapped]
[JsonProperty( "answers", NullValueHandling = NullValueHandling.Ignore )]
public List<string> DataAnswerStrings
{
    get => DataAnswers?.Select( da => da.AnswerText )?.ToList();
    set => DataAnswers = value
        ?.Select( s => new DataAnswer() { AnswerText = s } )
        ?.ToList() 
        ?? new List<DataAnswer>();
}
[JsonIgnore]
公共虚拟列表数据应答{get;set;}
[未映射]
[JsonProperty(“answers”,NullValueHandling=NullValueHandling.Ignore)]
公共列表数据应答字符串
{
get=>DataAnswers?.Select(da=>da.AnswerText)?.ToList();
set=>DataAnswers=value
?.选择(s=>newdataanswer(){AnswerText=s})
?ToList()
??新列表();
}