如何使用Npgsql设置JSON列?

问题描述 投票:0回答:1

我正在建立一个小型模型,并且从docs website执行代码时遇到此异常。

这是我的代码:

  public class SomeEntity
  {
    public int Id { get; set; }
    [Column(TypeName = "jsonb")]
    public Customer Customer { get; set; }
  }

  public class Customer    // Mapped to a JSON column in the table
  {
    public string Name { get; set; }
    public int Age { get; set; }
    public Order[] Orders { get; set; }
  }

  public class Order       // Part of the JSON column
  {
    public decimal Price { get; set; }
    public string ShippingAddress { get; set; }
  }
using (var dbContext = services.GetRequiredService<AppDbContext>())
{
  await dbContext.Database.MigrateAsync();

  dbContext.SomeEntities.Add(
    new SomeEntity
    {
      Customer = new Customer
      {
        Name = "Roji",
        Age = 35,
        Orders = new[]
        {
          new Order { Price = 3, ShippingAddress = "Somewhere" },
          new Order { Price = 3, ShippingAddress = "Nowhere" }
        }
      }
    });

  await dbContext.SaveChangesAsync();
}

当我打电话给SaveChanges时,出现以下异常:

Npgsql.PostgresException:42P01:关系\“ SomeEntities \”不存在

Here是一个repro项目。

因为我相信我已按照手册中的所有步骤进行操作,所以我也打开了一个问题here

c# json postgresql entity-framework-core npgsql
1个回答
1
投票

您正在调用MigrateAsync方法,但您的项目没有任何实际的迁移(可以使用dotnet ef migrations add <name>创建。如果您只是在玩耍,则可能要调用dbContext.Database.EnsureCreated,请参阅此文档页面。

© www.soinside.com 2019 - 2024. All rights reserved.