簡體   English   中英

我將如何 go 將 EFCore DBSet 保存為 JSON?

[英]How would I go about saving an EFCore DBSet as JSON?

希望在與 Entity Framework Core、.NET 5 的一些混淆方面指出正確的方向(如果重要的話)。

這個問題的上下文是一個“E2E”測試,因為缺少一個更好的術語,這是一個場景。 (ScenarioForm,發布在下面)要求有一個數據庫表,用於跟蹤給定場景及其執行狀態,以及一些不重要的指標。

此表單由服務/控制器提供,是 JSON object。 目前是 JSON,因為我們的“場景”還處於早期階段,並且希望能夠相當輕松地對其進行更改。 我從更高層的人那里得到了這個要求,所以請將此要求視為一個常數。

using System;
using Microsoft.EntityFrameworkCore;
using MyNamespace.Contracts.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace MyNamespace.Contracts.Forms
{
    /// <summary>
    /// Defines the execution parameters of a scenario.
    /// </summary>
    [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
    [Keyless] // This is me trying to figure things out, probably no need to include keyless..?
    public class ScenarioForm
    {
        /// <summary>
        /// Quantity of customers created by this scenario.
        /// </summary>
        public int CustomerCount { get; set; } = 1;

        /// <summary>
        /// Id of the Organization to use in this scenario
        /// </summary>
        public Guid OrganizationId { get; set; }

        /// <summary>
        /// Type of running, whether it be from a stream or file.
        /// </summary>
        public RunnerType RunnerType { get; set; } = RunnerType.File;
    }
}

我還有一個實體,它應該是給定場景狀態的表示。

using System;
using System.ComponentModel.DataAnnotations.Schema;
using MyNamespace.Contracts.Forms;
using MyNamespace.Contracts.Models;

namespace MyNamespace.Data.Entities
{
    public class ScenarioStatus
    {
        public Guid Id { get; set; }
        [NotMapped] // This is leftover from me trying stuff, I imagine this should be a different relationship.
        public ScenarioForm Specification { get; set; }
        public RunnerType RunnerType { get; set; }
        public DateTime? StartedAt { get; set; }
        public DateTime? CompletedAt { get; set; }
        public DateTime? ElapsedTime { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedAt { get; set; }
        public string UpdatedBy { get; set; }
        public DateTime? UpdatedAt { get; set; }
    }
}

我遇到的問題是我希望 ScenarioForm 只是 ScenarioStatus 表中的一列,作為它的 JSON 表示,而不是為 ScenarioForm 提供一個完全獨立的表。

我也知道通過屬性來定義這些關系並不理想,請隨意使用屬性或使用 FluentAPI 給出方向。

任何方向表示贊賞,謝謝!

我將代碼切碎了一點,所以如果有任何令人困惑的地方,請告訴我,我可以澄清一下。

根據您使用的數據庫,有兩種方法。

  1. 如果你容易使用 Postgres,你可以使用 jsonb 類型,它在數據庫中保存為 JSON。 有關更多信息, 請閱讀此
    public class ScenarioStatus
    {
        public Guid Id { get; set; }
        [Column(TypeName = "jsonb")]
        public ScenarioForm Specification { get; set; }
        public RunnerType RunnerType { get; set; }
        public DateTime? StartedAt { get; set; }
        public DateTime? CompletedAt { get; set; }
        public DateTime? ElapsedTime { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedAt { get; set; }
        public string UpdatedBy { get; set; }
        public DateTime? UpdatedAt { get; set; }
    }
  1. 使用轉換器將屬性轉換為 JSON
public class ScenarioStatusConfiguration : IEntityTypeConfiguration<ScenarioStatus>
{
    public void Configure(EntityTypeBuilder<ScenarioStatus> builder)
    {
        builder.Property(e => e.Specification).HasConversion(
            v => JsonConvert.SerializeObject(v),
            v => JsonConvert.DeserializeObject<Specification>(v)
        );
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM