[英]Entity Framework Core jsonb column type
I am using Entity Framework Core with npgsql postgresql for Entity Framework Core.我正在将 Entity Framework Core 与 npgsql postgresql 一起用于 Entity Framework Core。
My question is, using migrations, how do I mark a class property to generate a JSONB column type?我的问题是,使用迁移,如何标记类属性以生成 JSONB 列类型?
For example:例如:
public class MyTableClass
{
public int Id { get; set; }
// My JSONB column
public string Data { get; set; }
}
Thanks in advance.提前致谢。
Based on H. Herzl comment:基于 H. Herzl 评论:
My final solution was something like this:我的最终解决方案是这样的:
public class MyTableClass
{
public int Id { get; set; }
[Column(TypeName = "jsonb")]
public string Data { get; set; }
}
Migrations generated this:迁移产生了这个:
Data = table.Column<string>(type: "jsonb", nullable: true),
When updated the database with migrations, the Data column was created correctly with jsonb type.使用迁移更新数据库时,使用 jsonb 类型正确创建了数据列。
Thank you H. Herzl!谢谢 H. Herzl!
using string
as was suggested by @bruno.almeida is a nice solution but couldn't be queried .使用 @bruno.almeida 建议的
string
是一个不错的解决方案,但无法查询。
additional approaches are to use:其他方法是使用:
JsonDocument
or JsonElement)JsonDocument
或 JsonElement) JsonDocument
being my favorite since it could be queried, is flexible and fast to setup, eg: JsonDocument
是我最喜欢的,因为它可以被查询,设置灵活且快速,例如:
public JsonDocument Customer { get; set; }
more details at: https://www.npgsql.org/efcore/mapping/json.html更多详细信息,请访问: https : //www.npgsql.org/efcore/mapping/json.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.