Home > Blockchain >  .NET Core 5 to .NET Core 6 migration exception
.NET Core 5 to .NET Core 6 migration exception

Time:10-22

When I change a project's target framework from .NET Core 5 to .NET Core 6, the following code throws an exception, and it cannot be compiled.

if (Database.IsMySql())
{
    modelBuilder.MutableEntities()
                .Where(entity => entity.GetProperties().Any(DatabaseHelper.HasGeneratedTimestamp))
                .ForEach(entity =>
                {
                    var properties = entity.GetProperties().Where(DatabaseHelper.HasGeneratedTimestamp);
                    var table = entity.GetTableName();
                    var key = (entity.FindPrimaryKey() ?? throw new InvalidOperationException($"primary key not defined on {table}"))
                                     .Properties
                                     .Select(property => property.Name)
                                     .Apply(columns => columns.Count() > 1 ? $"({columns.Join(", ")})" : columns.First());
                    properties.Where(DatabaseHelper.IsOnInsert).ForEach(property =>
                    {
                        // property.SetDefaultValue(DateTimeOffset.Now);
                    });
                    properties.Where(DatabaseHelper.IsOnUpdate).ForEach(property =>
                    {
                        // // property.SetDefaultValue(DateTimeOffset.Now);
                        // if (property.ValueGenerated == ValueGenerated.OnAddOrUpdate)
                        //     property.SetDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
                    });
                });
        }
        

Where Any(DatabaseHelper.HasGeneratedTimestamp) throws the following error:

Cannot convert from 'method group' to 'Func<IMutableProperty, bool>

The intellisense of Any()

How can I fix this error? Do any packages need to be updated?

CodePudding user response:

It sounds like it is struggling to decide on a specific method overload - presumably Any gained some overloads, which is making things ambiguous; maybe help it:

.Any(prop => DatabaseHelper.HasGeneratedTimestamp(prop))

CodePudding user response:

The interface IMutableProperty has been changed between EFCore 5 and EFCore 6 from

public interface IMutableProperty: IMutablePropertyBase, IProperty

to

public interface IMutableProperty: IMutablePropertyBase, IReadOnlyProperty

Therefore the type IMutableProperty is not compatible to IProperty anymore. The simplest solution would be to change your helper method from

 public static bool HasGeneratedTimestamp(this IProperty property)

to

 public static bool HasGeneratedTimestamp(this IMutableProperty property)

or

 public static bool HasGeneratedTimestamp(this IReadOnlyProperty property)
  • Related