簡體   English   中英

EF Core Postgres 列表<t> .包含實體屬性</t>

[英]EF Core Postgres List<T>.Contains entity property

我遇到了存儲為數組 integer 的 EF Core Postgres 列表屬性的問題,.Contains() 方法沒有被正確翻譯。

實體:

 public class Test
    {
        public int Id { get; set; }
        public List<int> TestArray { get; set; }
    }

Ef 查詢:

var result = await _dbContext.Test
            .Where(x => x.TestArray.Contains(1)) //1 can be variable
            .ToListAsync();

Postgresql查詢:

SELECT *
      FROM "Test"
      WHERE (TRUE = FALSE)

將屬性類型從 List 更改為 int[] 效果很好,但 List 有很多好處。 甚至 postgres 文檔說 Contains 也受支持https://www.npgsql.org/efcore/mapping/array.html 有什么解決方法嗎?

UPD: TestArray 是 postgres 數組列整數 []

根據我的經驗,應將 EF collections 聲明為ICollection<XXX> ,並且所有示例都用於使用 HashSet 對其進行初始化。 您不應該使用[]因為它是一個固定大小的數組,並且您將無法向其中添加新記錄。

public class Test
{
    public int Id { get; set; }
    public ICollection<int> TestArray { get; set; } = new HashSet<int>();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM