Efcore.pg: Support seeding for JSON POCOs

Created on 30 Nov 2020  路  6Comments  路  Source: npgsql/efcore.pg

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.

enhancement

Most helpful comment

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.

All 6 comments

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

@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.

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

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.

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

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...

Was this page helpful?
0 / 5 - 0 ratings