【发布时间】:2020-08-13 19:04:56
【问题描述】:
我在 mysql 中创建了一个带有 JSON 列的数据库。 在 Swagger 中定义的 API。 将 JSON 写入数据库没有问题,但读取时,JSON 字段的值显示为转义字符串而不是 JSON
这是模型的一部分:
/// <summary>
/// Gets or Sets Doc
/// </summary>
[DataMember(Name="doc")]
public Dictionary<string, string> Doc { get; set; }
我也尝试过使用字符串类型和 Dictionary
获取方法在这里:
public virtual IActionResult GetDataById([FromRoute][Required]int? dataId, [FromRoute][Required]string jsonNode)
{
if(_context.Data.Any( a => a.Id == dataId)) {
var dataSingle = _context.Data.SingleOrDefault( data => data.Id == dataId);
return StatusCode(200, dataSingle);
} else {
return StatusCode(404);
}
}
}
生成的 JSON 响应看起来像这样:
{
"field1": "value1",
"field2": "value2",
"doc": "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":{\"subkey3-1\":\"value3-1\",\"subkey3-2\":\"value3-2\"}}"
}
但正确的 JOSN 应该是这个:
{
"field1": "value1",
"field2": "value2",
"doc": {
"key1":"value1",
"key2":"value2",
"key3":{
"subkey3-1":"value3-1",
"subkey3-2":"value3-2"
}
}
}
如果我尝试只返回“Doc”(JSON) 字段,则响应 JSON 格式正确。
我尝试了不同的序列化/反序列化但不成功。
如果我有 public Dictionary
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HM1VN0F0I0IM", Request id "0HM1VN0F0I0IM:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: The property 'Data.Doc' is of type 'Dictionary<string, string>' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
and for public string Doc { get;放; } 在模型中我得到了“doc”字段值作为我上面提到的转义字符串。
最好的方法是什么?
【问题讨论】:
-
据我所知,EF 不支持 JSON 列。应作为字符串放置并由 Json.NET 等 3rd 方库解析。
-
EF 自 2016 年起支持 JSON 类型。但现在这些都不起作用stackoverflow.com/questions/47455701/…
-
你是对的。那只是数据类型的别名。需要由其他库解析。连接器库支持您链接的案例,而不是 EF 本身。
标签: c# entity-framework .net-core