如何在EF Core中为拥有的JSON列添加注释?

huangapple go评论90阅读模式
英文:

How to add a comment to owned JSON column in EF Core?

问题

"HasComment"调用在尝试使用dotnet ef migrations add创建迁移时会导致失败。给出的消息是:

System.InvalidOperationException: 因为它被配置为导航,所以无法将'Metadata'用作实体类型'MyEntity'的属性。

我理解这个问题的原因可能是因为如果明天我选择将这个'Metadata'存储在单独的表中,那么就无法再有“列注释”了。如何覆盖此行为并添加注释呢?

我唯一想到的方法是手动编辑迁移以添加注释,但这意味着如果我们将来想要编辑注释,就必须再次手动编辑它,所以我更喜欢让EF Core为我们生成指令。

英文:

Given an EF Core entity with an owned type that's stored in a JSON column like so

public class MyEntity
{
  public MyEntity( SomeMetadata metadata) : this()
  {
    Metadata = metadata;
  }

  /// <summary>
  /// Just for EntityFramework because it can't handle a constructor with owned type.
  /// </summary>
  private MyEntity()
  {
  }

  public Guid PrimaryId { get; private set; }

  public SomeMetadata Metadata { get; private set; }

  public class SomeMetadata
  {
    public string? SomeNestedThing { get; set; }
  }
}


public class MyEntityConfiguration : IEntityTypeConfiguration<MyEntity>
{
  public void Configure(EntityTypeBuilder<MyEntity> builder)
  {
    builder.OwnsOne(e => e.Metadata, ownedNavigationBuilder =>
    {
      ownedNavigationBuilder.ToJson();
    });

    builder.Property(e => e.Metadata)
      .HasComment("Comment about this JSON column");
  }
}

The HasComment call will result in a failure when trying to create a migration using dotnet ef migrations add. The message given is

> System.InvalidOperationException: 'Metadata' cannot be used as a property on entity type 'MyEntity' because it is configured as a navigation.

I understand that the reason for this is probably because if tomorrow I choose to store this Metadata in a separate table, then I can't have a "column comment" anymore. How can I go about overriding this and adding a comment?

The only thing I've come up with is editing the migration manually to add a comment, but this means that if we want to edit a comment later in the future, we'd have to manually edit it again, so I'd prefer to let EF Core generate the instruction for us.

答案1

得分: 1

Metadata 不是表中的列,它是一个导航属性,应该指向另一张表中包含所需信息的记录。

然而,在您的情况下,您实际上不需要一个导航属性。您可以简单地调整您的代码,使 "JSON" 列成为一个字符串。

public MyEntity(SomeMetadata metadata) : this()
{
    Metadata = metadata.SomeNestedThing;
}

public string Metadata { get; private set; }

在进行这些更改之后,Metadata 将成为表中的一个实际列,您用于添加评论的代码将会起作用。

英文:

Metadata is not a column in a table, it is a navigation property, which should point to a record in another table that holds your desired info.

However, in your scenario, you don't really need a navigation property. You can just adjust your code so that the "JSON" column is a string.

public MyEntity(SomeMetadata metadata) : this()
{
	Metadata = metadata.SomeNestedThing;
}

public string Metadata { get; private set; }

After these changes, Metadata will be an actual column in the table and your code for adding the comment will work.

huangapple
  • 本文由 发表于 2023年3月7日 21:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定