npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.52k stars 223 forks source link

Support seeding for JSON POCOs #1595

Open mhosman opened 3 years ago

mhosman commented 3 years ago

I'm trying to use a POCO mapping (to use a jsonb column with a list of a defined object). When I try to run a migration I get an error:

Cannot scaffold C# literals of type 'System.Collections.Generic.List`1[Api.Models.UserType]'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.

Small project to reproduce the issue: WebApplication2.zip

The only way I've found to achieve this is to create a String and make the JsonSerializer.Serialize() by myself wich is not the best solution at all.

roji commented 3 years ago

Can you confirm that you're hitting this when trying to add a migration which seeds JSONB List values?

mhosman commented 3 years ago

@roji Not necessarily. In the code I attached there is exactly the same example from the documentation (it doesn't use a list, it uses the Customer object). In order to reproduce just open the code, run a migration and you'll get the error. If you remove the HasData part (this will not seed data), the code will work.

supapo commented 3 years ago

Hi, I have this problem too, any fix or workaround?

roji commented 3 years ago

This is currently unsupported; if you use JSON POCO mapping, you'll have to take care of seeding yourself.

The type mapping could use reflection to discover public properties which are readable and writable, and generate the proper C# code based on that. This will probably require some additions to code generation on the EF Core upstream side.

mhosman commented 3 years ago

Hey @roji thank you very much for you response. Is there any idea if this will be implemented in the future? Thank you

roji commented 3 years ago

As this may go well together with the general push for JSON support in EF Core, I've put this in the 6.0 milestone - but I wouldn't say this is the highest-priority item...

laggage commented 1 year ago

Encountered the problem, hoping to have this feature~

Amorganskate commented 7 months ago

Would be nice if this was bumped up in the list. I was able to find a sneaky away to get around flat jsonb objects but it seems like actual jsonb arrays still has an issue.