I have a few columns that contain JSON data. These particular columns are an array of enums.
When querying the MembershipType
table, the serialization and deserialization works well and as expected. However, I have a SQL view that nests an array of MembershipType
s and doing causes EF Core to throw an error similar to this:
System.Exception: "Error converting value \"[\"Certified\"]\" to type 'DataLayer.Models.Members.MembershipTypeCategory[]'. Path '[0].History.MembershipType.Categories', line 1, position 585."
You can see all the extra quotes and backslashes that are added to the value. I have tried string replacing the quotes and backslashes, which works, but this can have bad affects with other string data.
Any ideas?
Code below: SQL VIEW
SELECT
hist.*
, (
select * from Member.MembershipTypes
where id = hist.MembershipTypeId and Deleted = 0
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) as MembershipType
...
Enum
public enum MembershipTypeCategory
{
Type1,
Type2,
Type3
}
and the class it's on:
[Table("MembershipTypes", Schema = "Member")]
public class MembershipType : EntityBase, IMembershipTypeDto
{
...
[NotMapped]
public MembershipTypeCategory[] Categories { get; set; }
...
}
In the ModelBuilder I have:
var settings = new JsonSerializerSettings
{
ContractResolver = new IgnoreVirtualResolver(),
PreserveReferencesHandling = PreserveReferencesHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
TypeNameHandling = TypeNameHandling.All,
Formatting = Formatting.None,
Error = HandleDeserializationError
};
modelBuilder.Entity<MembershipType>()
.Property(x => x.Categories)
.HasConversion(
v => JsonConvert.SerializeObject(v, Formatting.None, settings),
v => JsonConvert.DeserializeObject<MembershipTypeCategory[]>(v, settings)
);
return modelBuilder;