linq2db.EntityFrameworkCore icon indicating copy to clipboard operation
linq2db.EntityFrameworkCore copied to clipboard

Mapping EF `HasConversion<string>` ignored

Open code-lime opened this issue 1 year ago • 0 comments

Linq2Db mapper ignore EF HasConversion<string> of property with type enum

public async Task<bool> UpdateTryFinishAsync(string uid, /* -- ANY ARGS -- */, CancellationToken cancellationToken)
{
    // -- ANY CODE --
    Payment? payment = await context.Payments.Where(v => v.Uid == uid).FirstOrDefaultAsync(cancellationToken); <-- ERROR
    // -- ANY CODE --
}

Throw:

LinqToDB.Common.LinqToDBConvertException: Mapping of column 'handle_status' value failed, see inner exception for details
 ---> System.FormatException: The input string 'Pending' was not in a correct format.
   at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)
   at lambda_method72(Closure, DbDataReader)
   at LinqToDB.Expressions.ConvertFromDataReaderExpression.ColumnReader.GetValue(DbDataReader dataReader)
   --- End of inner exception stack trace ---
   at LinqToDB.Expressions.ConvertFromDataReaderExpression.ColumnReader.GetValue(DbDataReader dataReader)
   at lambda_method61(Closure, IQueryRunner, DbDataReader)
   at LinqToDB.Linq.QueryRunner.Mapper`1.ReMapOnException(IDataContext context, IQueryRunner queryRunner, DbDataReader dataReader, ReaderMapperInfo& mapperInfo, Exception ex)
   at LinqToDB.Linq.QueryRunner.Mapper`1.Map(IDataContext context, IQueryRunner queryRunner, DbDataReader dataReader, ReaderMapperInfo& mapperInfo)
   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
   at LinqToDB.Linq.Builder.FirstSingleBuilder.FirstSingleContext.<>c__DisplayClass5_0`1.<<GetFirstOrDefaultElement>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at LinqToDB.Linq.ExpressionQuery`1.LinqToDB.Async.IQueryProviderAsync.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at LinqToDB.Linq.ExpressionQuery`1.LinqToDB.Async.IQueryProviderAsync.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at A2Core.Infrastructure.DataBase.PaymentRepository.UpdateTryFinishAsync(String uid, PaymentTimedData data, PaymentStatus status, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\DataBase\PaymentRepository.cs:line 29
   at A2Core.Infrastructure.Services.PaymentService.UpdatePaymentAsync(String uid, PaymentTimedData data, StatusType status, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Services\PaymentService.cs:line 189
   at A2Core.Infrastructure.Listeners.BePaidPaymentListener.DoPaymentAsync(IPaymentService paymentService, EripPayment payment, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Listeners\BePaidPaymentListener.cs:line 113
   at A2Core.Infrastructure.Listeners.BePaidPaymentListener.DoPaymentsAsync(IPaymentService paymentService, IEnumerable`1 payments, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Listeners\BePaidPaymentListener.cs:line 104
   at A2Core.Infrastructure.Listeners.BePaidPaymentListener.DoScopeTickAsync(IServiceProvider provider, Boolean isHandle, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Listeners\BePaidPaymentListener.cs:line 59
   at A2Core.Infrastructure.Listeners.BaseScopeLoopSerivce.DoTickAsync(Boolean isHandle, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Listeners\BaseScopeLoopSerivce.cs:line 15
   at A2Core.Infrastructure.Listeners.BaseScopeLoopSerivce.DoTickAsync(Boolean isHandle, CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Listeners\BaseScopeLoopSerivce.cs:line 15
   at A2Core.Infrastructure.Listeners.BaseLoopSerivce.DoLoopAsync(CancellationToken cancellationToken) in C:\GitHub\StudiaA2\a2-core\src\a2\A2Core.Infrastructure\Listeners\BaseLoopSerivce.cs:line 36

Domain entities:

public partial class Payment
{
    // -- ANY CODE --
    public PaymentHandleStatus HandleStatus { get; set; } = PaymentHandleStatus.Pending;
    // -- ANY CODE --
}


public enum PaymentHandleStatus
{
    Pending,
    // -- ANY CODE --
    Successful
}

DbContext:

public partial class ApplicationDbContext : DbContext, IContext
{
    public ApplicationDbContext() { }
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
    // -- ANY CODE --
    public DbSet<Payment> Payments { get; set; }
    // -- ANY CODE --
    ITable<Payment> IContext.Payments => Payments.ToLinqToDBTable();
    // -- ANY CODE --
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // -- ANY CODE --
        modelBuilder
            .Entity<Payment>(entity =>
            {
                // -- ANY CODE --
                entity.Property(e => e.HandleStatus)
                    .HasConversion<string>() <-- NOT WORK
                    .HasMaxLength(50);
                // -- ANY CODE --
            });
        // -- ANY CODE --
    }
    // -- ANY CODE --
}

Method LinqToDBForEFTools.Initialize(); executed before setup DB

Relevant Nuget Packages:

  • linq2db.EntityFrameworkCore: v8.0.0
  • linq2db: v5.3.2
  • Pomelo.EntityFrameworkCore.MySql: v8.0.1

code-lime avatar Mar 02 '24 13:03 code-lime