使用 Entity Framework Core 6 和 .NET Core 6 进行 PostgreSQL JSONB 序列化/反序列化
我正在使用 .NET 6 框架探索 Microsoft Entity Framework Core 6.0.3。
我的目标数据库是 PostgreSQL - 我正在使用 Npgsql.EntityFrameworkCore.PostgreSQL
(6.0.3)。
我有两个类,
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; }
}
我编写了以下代码来从 Employee
表中检索数据:
List<Employee> list = db.Employees.Where(x => x.Id == "1234").ToList();
我能够看到 list[0].Id
、list[0].Name
按预期填充。我还可以看到 list[0].Data[0]
可用。
问题是我看不到 list[0].Data[0].familyMembers
和 list[0].Data[0].phoneNumbers
。
问题是,在 PostgreSQL 数据库中,familyMembers
列实际上是 Family Members
,phoneNumbers
是电话号码
。
Employee
表的 Data
列设置为 jsonb
列,并具有以下数据:
[
{
"Family Members": ["Mother", "Father", "Wife"],
"Phone Numbers": ["9998887765", "1123444444"]
}
]
我的问题是:如何映射 Family Members将
属性放入我的 C# 类的 jsonb
列的familyMembers
数组中?
根据我的调查,如果我在数据库中的 JSON 中使用 familyMembers
属性,它可以
[
{
"familyMembers": ["Mother", "Father", "Wife"],
"phoneNumbers": ["9998887765", "1123444444"]
}
]
使用 JsonProperty("Family Members")
属性,但是不起作用。
public class ExtendedData
{
JsonProperty("Family Members")
public List<string> familyMembers { get; set; }
...
}
并非如此 - 即使在此示例中没有意义,Data
属性也必须是集合(列表类型)以及 familyMembers
和 phoneNumbers
是一个字符串列表。
谢谢, 阿图尔
I am exploring Microsoft Entity Framework Core 6.0.3 with the .NET 6 framework.
My target database is PostgreSQL - and I'm using 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:
List<Employee> list = db.Employees.Where(x => x.Id == "1234").ToList();
I able to see list[0].Id
, list[0].Name
populated as expected. I also able to see list[0].Data[0]
available.
Problem is I am NOT able to see list[0].Data[0].familyMembers
and list[0].Data[0].phoneNumbers
.
Problem is, in PostgreSQL database, familyMembers
column is actually Family Members
and phoneNumbers
is Phone Numbers
.
Data
column of Employee
table is set to jsonb
column and has the following data:
[
{
"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?
With my investigation, if I use familyMembers
property in JSON in database, it works
[
{
"familyMembers": ["Mother", "Father", "Wife"],
"phoneNumbers": ["9998887765", "1123444444"]
}
]
Using JsonProperty("Family Members")
attribute however didn't work.
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.
Thanks,
Atul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从 https://www.npgsql 得到提示。 org/efcore/mapping/json.html?tabs= Fluent-api%2Cpoco
我专注于 JsonProperty 但看起来像另一个属性JsonPropertyName 有效。
I got hint from https://www.npgsql.org/efcore/mapping/json.html?tabs=fluent-api%2Cpoco
I was focusing on JsonProperty but looks like another attribute JsonPropertyName worked.