[英]PostgreSQL JSONB serialization/de-serialization with Entity Framework Core 6 and .NET Core 6
I am exploring Microsoft Entity Framework Core 6.0.3 with the .NET 6 framework.我正在使用 .NET 6 框架探索 Microsoft Entity Framework Core 6.0.3。
My target database is PostgreSQL - and I'm using Npgsql.EntityFrameworkCore.PostgreSQL
(6.0.3).我的目标数据库是 PostgreSQL - 我正在使用
Npgsql.EntityFrameworkCore.PostgreSQL
(6.0.3)。
I have two classes我有两节课
public class ExtendedData
{
public List<string> familyMembers { get; set; }
public List<string> phoneNumbers { get; set; }
}
public class Employee
{
public String Id { get; set; }
public String Name { get; set; }
public List<ExtendedData> Data { get; set; }
}
I wrote following code to retrieve data from the Employee
table:我编写了以下代码来从
Employee
表中检索数据:
List<Employee> list = db.Employees.Where(x => x.Id == "1234").ToList();
I able to see list[0].Id
, list[0].Name
populated as expected.我能够看到
list[0].Id
, list[0].Name
按预期填充。 I also able to see list[0].Data[0]
available.我还可以看到
list[0].Data[0]
可用。
Problem is I am NOT able to see list[0].Data[0].familyMembers
and list[0].Data[0].phoneNumbers
.问题是我看不到
list[0].Data[0].familyMembers
和list[0].Data[0].phoneNumbers
。
Problem is, in PostgreSQL database, familyMembers
column is actually Family Members
and phoneNumbers
is Phone Numbers
.问题是,在 PostgreSQL 数据库中,
familyMembers
列实际上是Family Members
而phoneNumbers
是Phone Numbers
。
Data
column of Employee
table is set to jsonb
column and has the following data: Employee
表的Data
列设置为jsonb
列,数据如下:
[
{
"Family Members": ["Mother", "Father", "Wife"],
"Phone Numbers": ["9998887765", "1123444444"]
}
]
My question is: how do I map Family Members
property of the jsonb
column into familyMembers
array of my C# class?我的问题是:如何将
jsonb
列的 map Family Members
属性放入我的 C# class 的familyMembers
数组中?
With my investigation, if I use familyMembers
property in JSON in database, it works根据我的调查,如果我在数据库中使用
familyMembers
中的 familyMembers 属性,它就可以工作
[
{
"familyMembers": ["Mother", "Father", "Wife"],
"phoneNumbers": ["9998887765", "1123444444"]
}
]
Using JsonProperty("Family Members")
attribute however didn't work.但是,使用
JsonProperty("Family Members")
属性不起作用。
public class ExtendedData
{
JsonProperty("Family Members")
public List<string> familyMembers { get; set; }
...
}
Not that - even if it doesn't make sense in this example, Data
property has to be collection (List type) and familyMembers
and phoneNumbers
is a list of strings.不是这样——即使在这个例子中它没有意义,
Data
属性必须是集合(列表类型)并且familyMembers
和phoneNumbers
是一个字符串列表。
Thanks, Atul谢谢,阿图尔
I got hint from https://www.npgsql.org/efcore/mapping/json.html?tabs=fluent-api%2Cpoco我从https://www.npgsql.org/efcore/mapping/json.html?tabs=fluent-api%2Cpoco得到提示
I was focusing on JsonProperty but looks like another attribute JsonPropertyName worked.我专注于JsonProperty ,但看起来另一个属性JsonPropertyName起作用了。
public class ExtendedData
{
[System.Text.Json.Serialization.JsonPropertyName("Family Members")]
public List<string> familyMembers { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("Phone Numbers")]
public List<string> phoneNumbers { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.