使用 Entity Framework Core 6 和 .NET Core 6 进行 PostgreSQL JSONB 序列化/反序列化

发布于 2025-01-14 13:21:44 字数 1940 浏览 2 评论 0原文

我正在使用 .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].Idlist[0].Name 按预期填充。我还可以看到 list[0].Data[0] 可用。

问题是我看不到 list[0].Data[0].familyMemberslist[0].Data[0].phoneNumbers

问题是,在 PostgreSQL 数据库中,familyMembers 列实际上是 Family MembersphoneNumbers电话号码

Employee 表的 Data 列设置为 jsonb 列,并具有以下数据:

[
    { 
      "Family Members": ["Mother", "Father", "Wife"], 
      "Phone Numbers": ["9998887765", "1123444444"]
    }
]

我的问题是:如何映射 Family Members将 jsonb 列的 属性放入我的 C# 类的 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 属性也必须是集合(列表类型)以及 familyMembersphoneNumbers是一个字符串列表。

谢谢, 阿图尔

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2025-01-21 13:21:44

我从 https://www.npgsql 得到提示。 org/efcore/mapping/json.html?tabs= Fluent-api%2Cpoco

我专注于 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; }
}

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.

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; }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文