添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We previously had model creating overrides for complex objects to store in jsonb columns. Previously the text->jsonb convert works but in the preview7 it's throwing an exception

protected override void OnModelCreating(ModelBuilder modelBuilder)
	base.OnModelCreating(modelBuilder);
	modelBuilder.Entity<Document>().Property(e => e.TableOfContents) // TableOfContents is a List<TableOfContents> complex object
		.HasColumnType("jsonb")
		.HasConversion(
			v => JsonConvert.SerializeObject(v,
				new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
			v => JsonConvert.DeserializeObject<List<TableOfContents>>(v,
				new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));

Error Message
PostgresException: 42804: column "table_of_contents" is of type json but expression is of type text

I've tried using but it doesn't appear to make a difference (assuming I set it up correctly)
https://www.npgsql.org/doc/types/jsonnet.html

Any thoughts?

This is in a seed method during DB initialization.

Just doing AddRangeAsync from a Deserialized JSON document await _context.Document.AddRangeAsync(documents);

var sampleDocsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sample-documents.json");
                var json = File.ReadAllText(sampleDocsPath);
                var documents = JsonConvert.DeserializeObject<IEnumerable<Document>>(json);
                if (documents is null || !documents.Any())
                    throw new Exception("Failed to seed with sample documents");
                    await _context.Document.AddRangeAsync(documents);
                    await _context.SaveChangesAsync();
          

@jsheely could you help out reproduce this? The below code seems to work fine, doing both types of seeding and a query. A simple self-contained code sample would be best, and since preview8 is about to come out it would be great if this could be quick :)

class Program
    static void Main(string[] args)
        using (var ctx = new BlogContext())
            ctx.Database.EnsureDeleted();
            ctx.Database.EnsureCreated();
            ctx.Blogs.Add(new Blog { Name = "WAT", Json = @"{ ""a"" : 1 }" });
            ctx.SaveChanges();
        using (var ctx = new BlogContext())
            var x = ctx.Blogs.Where(b => b.Json == @"{ ""a"" : 1 }").ToList();
public class BlogContext : DbContext
    public DbSet<Blog> Blogs { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        optionsBuilder.UseNpgsql("Host=localhost;Database=test;Username=npgsql_tests;Password=npgsql_tests");
    protected override void OnModelCreating(ModelBuilder modelBuilder)
        modelBuilder.Entity<Blog>().ToTable("blog")
            .Property(b => b.Json).HasColumnType("jsonb").IsRequired();
        modelBuilder.Entity<Blog>().HasData(new Blog { Id = -1, Name = "WAT", Json = @"{ ""a"" : 1 }" });
public class Blog
    public int Id { get; set; }
    public string Name { get; set; }
    public string Json { get; set; }
          

Okay great. I'll take a look. Though the one thing I notice right from the onset is that Blog.Json is of type string where in my case our json is List.

Stored in the DB is a json array. Will see if that makes any difference.

I did confirm that yours does work for me with the string. It seems specific to the IList

Here is a repo where it works in 2.2.6 of EF Core and breaks in preview7

https://github.com/jsheely/ef-core-json
https://github.com/jsheely/ef-core-json/tree/preview7

Only difference between the two branches is the packages

Doing a little more testing though it's interesting that it works with HasData(). It seems that only ctx.SaveChanges() breaks it.

I also can confirm that it can read the data just fine.
var x = ctx.Blogs.Where(b => b.Id == -1).ToList();