Type 'string[]' which is not a supported primitive type or a valid entity type. - How do I deserialize the json string?
11,293
You can try using this online json to C# class generator to get the structure of a POCO required to deserialize a json string.
Then simply map it to your EF class (using AutoMapper for example):
So for this JSON:
{
"CustomFields": ['test','test2'],
"SampleNumber":"123"
}
this is the POCO that is generated:
public class RootObject
{
public List<string> CustomFields { get; set; }
public string SampleNumber { get; set; }
}
if this is your actual JSON:
{ "PayloadData":
{
"CustomFields": ['test','test2'],
"SampleNumber":"123"
}
}
this is how your POCO is supposed to look like:
public class PayloadData
{
public List<string> CustomFields { get; set; }
public string SampleNumber { get; set; }
}
public class RootObject
{
public PayloadData PayloadData { get; set; }
}
Author by
kudlatiger
Updated on June 16, 2022Comments
-
kudlatiger almost 2 years
I have below
json
"PayloadData": { "CustomFields": ['test','test2'], "SampleNumber":"123" }
I am using below code to deserialize the json.
Message message = JsonConvert.DeserializeObject<Message>(payloadData);
Here is my property in
Message
class/// <summary> /// Gets or sets CustomFields /// </summary> [Required] [DataMember(Name = "CustomFields")] public List<string> CustomFields{ get; set; }
But, I get below error.
"The property 'Message.CustomFields' could not be mapped, because it is of type 'string[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."
-
kudlatiger about 5 yearsI am using entity framework and It's nothing to do with JSON serialization. Now, I am wondering how to store this list in database!
-
JustLearning about 5 yearsyour payload data is a json string? Right? then you are trying to deserialize it into an EF code first class? If so, i am suggesting to first deserialize it to a POCO then map it to which ever EF class you require.
-
Wylan Osorio almost 3 years@kudlatiger List or Array is not supported as a column type in the SQL database. You need make it as a separate class so it will be a separate table in the database. Let's say CustomField class with ID and Field properties, then update the Message class's CustomFields prop to be list of CustomField type.