EF Core 7 Json 数组的列映射

Dew*_*823 6 c# entity-framework entity-framework-core

我有一个带有 json 列“Extras”的 sql 表

  [{"Key":"Part Number","Value":123},{"Key":"Batch Number","Value":321},{"Key":"Quantity","Value":"3"}]
Run Code Online (Sandbox Code Playgroud)

我正在尝试将其映射到我的班 LinerReceiptLog。我添加了一个名为 tag 的新类,它具有 Key 和 Value 属性

public partial class LinerReceiptLog
{
    public long LinerReceiptLogId { get; set; }

    public int EmployeeId { get; set; }

    public DateTime LogDate { get; set; }

    public int TotalQty { get; set; }

    public string Ncrnumber { get; set; }

    public List<Tag> Extras { get; set; }

    public virtual Employee Employee { get; set; }
}

public class Tag
{
    public string Key { get; set; }
    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我更新了模型生成器

entity.OwnsMany(e => e.Extras, builder=>builder.ToJson());
Run Code Online (Sandbox Code Playgroud)

当我运行代码来查询数据库上下文时,我收到此异常。

System.InvalidOperationException: 'The requested operation requires an element of type 'Array', but the target element has type 'Object'.'
Run Code Online (Sandbox Code Playgroud)

我一直在关注这篇文章 https://blog.jetbrains.com/dotnet/2023/02/14/getting-started-entity-framework-core-7-json-support/