変にハマったので記録
EFCore7から使えるようになったJSON列
-
https://learn.microsoft.com/ja-jp/ef/core/what-is-new/ef-core-7.0/whatsnew
↑ 公式より
これを使って追加情報とかをJSONでまとめとこうとした。
モデル
public class Customer
{
public string Name { get; set; }
public Status Status { get; set; }
}
public class Status
{
// これはJSONマッピングする
public CustomerAdditionalDetail Detail { get; set; }
}
public class CustomerAdditionalDetail
{
// 後で定義しようとした
}
みたいな感じで、(modelbuilderのあたりは省略)
で、includeすると
context.Customers
.Include(c => c.Status);
エラーが発生
System.ArgumentOutOfRangeException
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
デバッグ見るとクエリの発行すらされていない、なので何かリレーションが間違ってるのか…
と見直したが特におかしいところはない。
原因
後で中身を定義しようとしたCustomerAdditionalDetail
が原因だった。
とりあえず適当なプロパティを入れると問題なく出るようになった。
(いろいろググってもわからなかったので記録のため記事にしておいた)