[英]Mapping a semi-structured JSON column into a class in EF Core 7
我想使用 EF Core 7 的新 JSON 列功能在我的 PostgreSQL 數據庫中存儲和檢索以下格式的數據:
{
"Name": "Email_AND_Phone_OR_RootUser",
"Rules": [
["HasEmail", "HasPhone"],
["IsRoot"]
]
}
這個字符串數組 arrays 具有動態長度,其中也包含字符串 arrays。 如果我理解正確,我應該創建一個擁有的 class,在我的實體中引用它並添加適當的數據屬性或將其配置為 OnModelCreating。 在我在 inte.net 中找到的示例中,我沒有看到在 JSON 映射 class 中使用列表或 arrays。以下映射類是否有效?
public class Policy
{
[MaxLength(30)] public string Name { get; set; } = null!;
public List<List<string>> RuleBinding { get; set; } = new();
}
或者,作為字符串數組 arrays:
public class Policy
{
[MaxLength(30)] public string Name { get; set; } = null!;
public string[][] RuleBinding { get; set; } = null!;
}
另外,在這種情況下使用 JSON 列是否合適?
PostgreSQL ( Npgsql.EntityFrameworkCore.PostgreSQL
) 的 EF Core 庫本身支持 JSON,它是在 EF Core 7 之前構建的,目前尚不支持 EF 7 API - 請參閱JSON 映射文檔:
EF Core 7.0 引入了對 JSON 列的支持。 Npgsql 的 JSON 支持 - 詳見下文 - 是不同的,並且從 3.0 版開始可用。 我們計划在 8.0 版本中采用 EF 的 JSON 支持。
AFAIK 應該支持這兩種映射。
public class Policy
{
[MaxLength(30)] public string Name { get; set; } = null!;
[Column(TypeName = "jsonb")] public List<List<string>> RuleBinding { get; set; } = new();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.