实体框架不保存JSONB属性

发布于 2025-02-11 02:41:53 字数 1234 浏览 3 评论 0原文

我有一个更新方法:

    public class User
    {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }
    [Column(TypeName = "jsonb")]
    }

     public async Task<bool> Update(int id, UserRequest user)
     {

      var result = await _context.Users.Where(x => x.Id == id).FirstOrDefaultAsync();

      var cities = new List<string>();
      cities.Add("New York");
      cities.Add("London");

            result = new Users()
            {
               Id = result.Id,
               FirstName = user.FirstName,
               LastName = user.LastName,
               City = JsonSerializer.Serialize(cities)
            };

         //  _context.Entry(result).Property(x => x.City).IsModified = true;

         await _context.SaveChangesAsync();

         return true;
      }

名称和姓氏更改反映在数据库中,但在城市数据库中不反映。

  _context.Entry(result).Property(x => x.City).IsModified = true;

作为解决方案,通常使用了输入功能,但是条目属性为我丢下错误:

计算数据库错误页面内容时发生了例外。
跳过数据库错误页面的显示。
system.invalidoperationException:
由于响应已经启动,因此无法设置状态代码。

I have an update method:

    public class User
    {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }
    [Column(TypeName = "jsonb")]
    }

     public async Task<bool> Update(int id, UserRequest user)
     {

      var result = await _context.Users.Where(x => x.Id == id).FirstOrDefaultAsync();

      var cities = new List<string>();
      cities.Add("New York");
      cities.Add("London");

            result = new Users()
            {
               Id = result.Id,
               FirstName = user.FirstName,
               LastName = user.LastName,
               City = JsonSerializer.Serialize(cities)
            };

         //  _context.Entry(result).Property(x => x.City).IsModified = true;

         await _context.SaveChangesAsync();

         return true;
      }

FirstName and LastName change is reflected in db but not in City db.

  _context.Entry(result).Property(x => x.City).IsModified = true;

As a solution, generally used the Entry feature, but the Entry property throws an error for me:

An exception occurred while calculating the database error page content.
Skipping display of the database error page.
System.InvalidOperationException:
StatusCode cannot be set because the response has already started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2025-02-18 02:41:53

我不明白问题的原因,但是以下更改解决了我的问题。

    result = new Users()
    {
       Id = result.Id,
       FirstName = user.FirstName,
       LastName = user.LastName,
       City = JsonSerializer.Serialize(cities)
    };

      result.FirstName = user.FirstName;
      result.LastName = user.LastName ;
      result.City = JsonSerializer.Serialize(cities);

I don't understand the cause of the issue but the following change solved my issue.

    result = new Users()
    {
       Id = result.Id,
       FirstName = user.FirstName,
       LastName = user.LastName,
       City = JsonSerializer.Serialize(cities)
    };

to

      result.FirstName = user.FirstName;
      result.LastName = user.LastName ;
      result.City = JsonSerializer.Serialize(cities);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文