Skip to Main Content

ODP.NET

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Migrations fails since EF Core 7.0.4

Hi,

Yesterday I did a minor dotnet upgrade from 7.0.3 to 7.0.4 and suddenly I couldn't run/create any migrations. Because of the following error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: The comparer for type 'double?' cannot be used for 'Customer.Latitude' because its type is 'decimal'.
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateTypeMappings(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Oracle.EntityFrameworkCore.Infrastructure.Internal.OracleModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
   ...

The EntityTypeConfiguration is the following:

builder.Property<double?>(e => e.Latitude)
       .HasColumnName("POS_Y")
       .HasColumnType("NUMBER(8,5)")
       .HasPrecision(8, 5);

The property is of type double? and the column is a NUMBER.

I think the problem is caused by the following change in EF Core https://github.com/dotnet/efcore/commit/f4e549ead524b1fcb3514ad49ce85fbef328b564 which is a fix for https://github.com/dotnet/efcore/issues/29985.

Somehow EF Core returns another ClrType or ProviderClrType that makes the OracleModelValidator.Validate fail.

I was able to fix this by adding a HasConversion<decimal?>.

builder.Property<double?>(e => e.Latitude)
       .HasConversion<decimal?>()
       .HasColumnName("POS_Y") 
       .HasColumnType("NUMBER(8,5)") 
       .HasPrecision(8, 5);

I only need to add this HasConversion<decimal?> on fields where NUMBER is used in the database and int, long, double, … is used in code. I'm not a 100% sure, but I think it is also only Nullable types that are impacted.

Can you verify if the workaround is valid and won't cause any additional problems? Maybe adjustments are needed on Oracle.EntityFrameworkCore or EF Core itself.

FYI, I cross-posted this on https://github.com/dotnet/efcore/issues/30503.

Regards,

Jochen

This post has been answered by Alex Keh-Oracle on Apr 20 2023
Jump to Answer
Comments
Post Details
Added on Mar 16 2023
3 comments
681 views