在PG中这是可能的:
public class Parent
{
[Column(TypeName = "jsonb")]
//Mode 1: a column in the table
public Child[] Children { get; set; }
}
public class Child
{
//Mode 2: a virtual column only existing in JSON
public GrandChild[] GrandChildren { get; set; }
}
public class GrandChild
{
}
我的问题是,是否有一种方法可以内联使用其他CLR类型,而不是使用数组,例如List<T>
,HashSet<T>
甚至justIList<T>
或ICollection<T>
,以方便访问并避免每次我们要进行更改或避免定义时重新创建集合一堆其他代理属性。
我尝试设置HasConversion
数组,但是没有用。
如果启用type plugins
in,Npgsql.Json.NET
则“自动”运行:
NpgsqlConnection.GlobalTypeMapper.UseJsonNet();
using (var context = new MyContext(options.Options))
{
var parent = new Parent()
{
Children = {
new Child() {
GrandChildren = {
new GrandChild() { Name = "A" },
new GrandChild() { Name = "B" }
}
}
}
};
context.Add(parent);
context.SaveChanges();
foreach(var p in context.Parents.ToList()) {
// This is just to print the whole object. You don't have to touch JSON.NET
// yourself here, Npgsql will convert to/from .net types at 'the edges'.
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(p));
}
}
// Using these definitions
class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{ }
public DbSet<Parent> Parents { get; set; }
}
public class Parent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column(TypeName = "jsonb")]
public List<Child> Children { get; set; } = new List<Child>();
}
public class Child
{
public List<GrandChild> GrandChildren { get; set; } = new List<GrandChild>();
}
public class GrandChild
{
public string Name { get; set; }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句