![](/img/trans.png)
[英]Entity Framework Core RowVersion column not updating using PostgreSQL
[英]Set formula for Computed column of PostgreSQL table in Entity Framework Core
我有一個帶有 Id 和 json 屬性的簡單模型和一個計算列,它是 json 列中'tile'鍵的值
public class Book
{
public int Id { get; set; }
[Column(TypeName="jsonb")]
public string JsonInfo { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Title { get; set; }
}
在配置中,我設置了計算Title
值的方式:
builder.Property(t => t.Title).HasComputedColumnSql("(\"JsonInfo\"->>''title'')", stored:true).HasColumnType("varchar(150)");
遷移構建沒有問題,但不幸的是數據庫更新以錯誤結束:
42601: syntax error at or near "title"
問題出在title
如何轉義的"(\\"JsonInfo\\"->>''title'')"
中。 我該如何解決?
順便提一下,我使用最新版本的 PostgreSQL
你不應該用兩次單引號來逃避標題; 您提供給 HasComputedColumnSql 的 SQL 不是文本文字,而是實際的 SQL。 所以以下應該工作:
.HasComputedColumnSql("\"JsonInfo\"->>'title'", stored: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.