<legend id='Rrgjv'><style id='Rrgjv'><dir id='Rrgjv'><q id='Rrgjv'></q></dir></style></legend>

      <bdo id='Rrgjv'></bdo><ul id='Rrgjv'></ul>

    <small id='Rrgjv'></small><noframes id='Rrgjv'>

    <i id='Rrgjv'><tr id='Rrgjv'><dt id='Rrgjv'><q id='Rrgjv'><span id='Rrgjv'><b id='Rrgjv'><form id='Rrgjv'><ins id='Rrgjv'></ins><ul id='Rrgjv'></ul><sub id='Rrgjv'></sub></form><legend id='Rrgjv'></legend><bdo id='Rrgjv'><pre id='Rrgjv'><center id='Rrgjv'></center></pre></bdo></b><th id='Rrgjv'></th></span></q></dt></tr></i><div id='Rrgjv'><tfoot id='Rrgjv'></tfoot><dl id='Rrgjv'><fieldset id='Rrgjv'></fieldset></dl></div>
    <tfoot id='Rrgjv'></tfoot>
      1. 如何使用 EF Core 将 JSON 存储在实体字段中?

        时间:2023-08-24
            <bdo id='og6Nq'></bdo><ul id='og6Nq'></ul>

            1. <legend id='og6Nq'><style id='og6Nq'><dir id='og6Nq'><q id='og6Nq'></q></dir></style></legend>
                <tbody id='og6Nq'></tbody>
              • <tfoot id='og6Nq'></tfoot>

                  <i id='og6Nq'><tr id='og6Nq'><dt id='og6Nq'><q id='og6Nq'><span id='og6Nq'><b id='og6Nq'><form id='og6Nq'><ins id='og6Nq'></ins><ul id='og6Nq'></ul><sub id='og6Nq'></sub></form><legend id='og6Nq'></legend><bdo id='og6Nq'><pre id='og6Nq'><center id='og6Nq'></center></pre></bdo></b><th id='og6Nq'></th></span></q></dt></tr></i><div id='og6Nq'><tfoot id='og6Nq'></tfoot><dl id='og6Nq'><fieldset id='og6Nq'></fieldset></dl></div>

                  <small id='og6Nq'></small><noframes id='og6Nq'>

                  本文介绍了如何使用 EF Core 将 JSON 存储在实体字段中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 .NET Core(针对 .NETStandard 1.4)创建一个可重用的库,并且我正在使用 Entity Framework Core(两者都是新的).我有一个看起来像这样的实体类:

                  I am creating a reusable library using .NET Core (targeting .NETStandard 1.4) and I am using Entity Framework Core (and new to both). I have an entity class that looks like:

                  public class Campaign
                  {
                      [Key]
                      public Guid Id { get; set; }
                  
                      [Required]
                      [MaxLength(50)]
                      public string Name { get; set; }
                  
                      public JObject ExtendedData { get; set; }
                  }
                  

                  我有一个定义 DbSet 的 DbContext 类:

                  and I have a DbContext class that defines the DbSet:

                  public DbSet<Campaign> Campaigns { get; set; }
                  

                  (我也在 DI 中使用 Repository 模式,但我认为这无关紧要.)

                  (I am also using the Repository pattern with DI, but I don't think that is relevant.)

                  我的单元测试给了我这个错误:

                  My unit tests give me this error:

                  System.InvalidOperationException:无法确定关系由类型的导航属性JToken.Parent"表示'JContainer'.要么手动配置关系,要么忽略这个来自模型的属性..

                  System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'JToken.Parent' of type 'JContainer'. Either manually configure the relationship, or ignore this property from the model..

                  有没有办法表明这不是一个关系,而是应该存储为一个大字符串?

                  Is there a way to indicate that this is not a relationship but should be stored as a big string?

                  推荐答案

                  @Michael 的回答让我走上了正轨,但我的实现方式略有不同.我最终将该值作为字符串存储在私有属性中,并将其用作支持字段".ExtendedData 属性然后将 JObject 转换为 set 上的字符串,反之亦然:

                  @Michael's answer got me on track but I implemented it a little differently. I ended up storing the value as a string in a private property and using it as a "Backing Field". The ExtendedData property then converted JObject to a string on set and vice versa on get:

                  public class Campaign
                  {
                      // https://docs.microsoft.com/en-us/ef/core/modeling/backing-field
                      private string _extendedData;
                  
                      [Key]
                      public Guid Id { get; set; }
                  
                      [Required]
                      [MaxLength(50)]
                      public string Name { get; set; }
                  
                      [NotMapped]
                      public JObject ExtendedData
                      {
                          get
                          {
                              return JsonConvert.DeserializeObject<JObject>(string.IsNullOrEmpty(_extendedData) ? "{}" : _extendedData);
                          }
                          set
                          {
                              _extendedData = value.ToString();
                          }
                      }
                  }
                  

                  要将 _extendedData 设置为支持字段,我将其添加到我的上下文中:

                  To set _extendedData as a backing field, I added this to my context:

                  protected override void OnModelCreating(ModelBuilder modelBuilder)
                  {
                      modelBuilder.Entity<Campaign>()
                          .Property<string>("ExtendedDataStr")
                          .HasField("_extendedData");
                  }
                  

                  更新:Darren 使用 EF Core Value Conversions 的答案(EF Core 2.1 的新功能 - 在此答案时不存在)似乎是目前最好的方法.

                  Update: Darren's answer to use EF Core Value Conversions (new to EF Core 2.1 - which didn't exist at the time of this answer) seems to be the best way to go at this point.

                  这篇关于如何使用 EF Core 将 JSON 存储在实体字段中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 Newtonsoft.Json.Linq.JArray 转换为特定对象类型的列 下一篇:在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?

                  相关文章

                  最新文章

                  <legend id='SaYPR'><style id='SaYPR'><dir id='SaYPR'><q id='SaYPR'></q></dir></style></legend>
                  <tfoot id='SaYPR'></tfoot>

                    1. <i id='SaYPR'><tr id='SaYPR'><dt id='SaYPR'><q id='SaYPR'><span id='SaYPR'><b id='SaYPR'><form id='SaYPR'><ins id='SaYPR'></ins><ul id='SaYPR'></ul><sub id='SaYPR'></sub></form><legend id='SaYPR'></legend><bdo id='SaYPR'><pre id='SaYPR'><center id='SaYPR'></center></pre></bdo></b><th id='SaYPR'></th></span></q></dt></tr></i><div id='SaYPR'><tfoot id='SaYPR'></tfoot><dl id='SaYPR'><fieldset id='SaYPR'></fieldset></dl></div>
                        <bdo id='SaYPR'></bdo><ul id='SaYPR'></ul>

                      <small id='SaYPR'></small><noframes id='SaYPR'>