linq2db / linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
MIT License
449 stars 39 forks source link

Not handling JSON-conversion of IList<string>, a native feature in EFCore #390

Closed killnine closed 3 months ago

killnine commented 4 months ago

Hello,

I'm very new to Linq2DB and this EFCore plugin for it. However, I noticed when trying to implement bulk-insert that some columns that otherwise work in EFCore fail with the following exception:

The given value 'System.Collections.Generic.List1[System.String]' of type List1 from the data source cannot be converted to type nvarchar for Column 14 [OrderNumbers].

The class which contains the property in question is set up like this:

public IList<string> OrderNumbers { get; set; } = new List<string>();

While the Field in the (SQL Server) database is an nvarchar(max).

The class which configures the table for this class is setup like this:

builder.Property(x => x.OrderNumbers)
                .HasColumnOrder(15);

Is MappingSchema.Default.SetValueToSqlConverter(typeof(IList<string>), ...) an appropriate method to convert lists of strings to a nvarchar? Has anyone done something similar?

killnine commented 4 months ago

I've tried calling this in my unit test which is testing the bulk-insert functionality but it's a no-go so far:

        MappingSchema.Default.SetConverter<List<string>, object>(list => JsonSerializer.Serialize(list));
        MappingSchema.Default.SetConverter<List<string>, SqlString>(list => JsonSerializer.Serialize(list));
        MappingSchema.Default.SetConverter<List<string>, string>(list => JsonSerializer.Serialize(list));
        MappingSchema.Default.SetConverter<IList<string>, object>(list => JsonSerializer.Serialize(list));
        MappingSchema.Default.SetConverter<IList<string>, SqlString>(list => JsonSerializer.Serialize(list));
        MappingSchema.Default.SetConverter<IList<string>, string>(list => JsonSerializer.Serialize(list));
killnine commented 3 months ago

Figured it out and posted solution here: https://stackoverflow.com/questions/78255883/not-handling-json-conversion-of-iliststring-into-database