LINQ表达式无法翻译,将在本地进行评估

时间:2019-10-28 11:34:28

标签: c# entity-framework-core

当使用具有自定义类型属性的实体时,该类型不能转换为SQL。

我创建了一个示例来说明我的解决方法:

某学期上一堂课。该学期以DateTime值的形式存储在数据库中。

学期本身是一种自定义类型,具有其他属性。

public class Semester 
{
   public enum HalfYear 
   {
      First = 1,
      Second = 7
   }

   DateTime _dateTime;

   public Semester (HalfYear halfYear, int year) 
   {
       _dateTime = new DateTime(year, (int) halfYear, 1)
   }

   public int Year => _dateTime.Year;
   public HalfYear HalfYear => (HalfYear) _dateTime.Month;
   public DateTime FirstDay => new DateTime(Year, _dateTime.Month, 1);
   public DateTime LastDay => new DateTime(Year, _dateTime.Month + 5, DateTime.DaysInMonth(Year, _dateTime.Month + 5));
}

public class Class 
{
   int Id { get; set; }
   string Title { get; set; }
   Semester Semester { get; set; }
}

可以使用value convertersSemester类型映射到DateTime

这在Where子句(例如

)中不起作用
db.Classes.Where(c = c.Semester.FirstDay <= DateTime.Now && 
                     c.Semester.LastDay >= DateTime.Now)

当Entity Framework Core尝试将表达式树转换为SQL时,它不知道如何转换Semester.FirstDaySemester.LastDay

这是文档中指出的值转换的已知限制

  

使用值转换可能会影响EF Core将表达式转换为SQL的能力。在这种情况下将记录警告。正在考虑删除这些限制以用于将来的版本。

如何解决此问题?

1 个答案:

答案 0 :(得分:3)

EntityFrameworkCore具有3个扩展点,可用于将自定义类型转换为SQL。

  • IMemberTranslator
  • IMethodCallTranslator
  • RelationalTypeMapping

可以使用相应的插件注册这些翻译和映射:

  • IMemberTranslatorPlugin
  • IMethodCallTranslatorPlugin
  • IRelationalTypeMappingSourcePlugin

插件已通过 IDbContextOptionsExtension

注册

以下示例说明了我如何实现这些接口来注册自定义类型Semester:

IMemberTranslator

public class SqlServerSemesterMemberTranslator : IMemberTranslator
{
    public Expression Translate(MemberExpression memberExpression)
    {

        if (memberExpression.Member.DeclaringType != typeof(Semester)) {
            return null;
        }

        var memberName = memberExpression.Member.Name;

        if (memberName == nameof(Semester.FirstDay)) {
            return new SqlFunctionExpression(
                "DATEFROMPARTS",
                typeof(DateTime),
                new Expression[] {
                        new SqlFunctionExpression( "YEAR", typeof(int),new[] { memberExpression.Expression }),
                        new SqlFunctionExpression( "MONTH", typeof(int),new[] { memberExpression.Expression }),
                        Expression.Constant(1, typeof(int))
                });
        }

        if (memberName == nameof(Semester.LastDay)) {
            return new SqlFunctionExpression(
                "EOMONTH",
                typeof(DateTime),
                new Expression[] {
                        memberExpression.Expression
                });
        }

        if (memberName == nameof(Semester.HalfYear)) {
            return Expression.Convert(
                new SqlFunctionExpression(
                    "MONTH",
                    typeof(int),
                    new Expression[] {
                            memberExpression.Expression
                    }),
                typeof(HalfYear));
        }

        if (memberName == nameof(Semester.Year)) {
            return new SqlFunctionExpression(
                "YEAR",
                typeof(int),
                new Expression[] {
                        memberExpression.Expression
                });
        }

        return null;
    }

}

IMethodCallTranslator

 public class SqlServerSemesterMethodCallTranslator : IMethodCallTranslator
{
    public Expression Translate(MethodCallExpression methodCallExpression)
    {
        if (methodCallExpression.Method.DeclaringType != typeof(Period)) {
            return null;
        }

        var methodName = methodCallExpression.Method.Name;

        // Implement your Method translations here

        return null;
    }
}

RelationalTypeMapping

 public class SqlServerSemesterTypeMapping : DateTimeTypeMapping
{
    public SqlServerSemesterTypeMapping(string storeType, DbType? dbType = null) : 
        base(storeType, dbType)
    {
    }

    protected SqlServerSemesterTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters)
    {
    }

    protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) => new SqlServerSemesterTypeMapping(parameters);
}

IMemberTranslatorPlugin

 public class SqlServerCustomMemberTranslatorPlugin : IMemberTranslatorPlugin
{
    public IEnumerable<IMemberTranslator> Translators => new IMemberTranslator[] { new SqlServerSemesterMemberTranslator() };
}

 public class SqlServerCustomMethodCallTranslatorPlugin : IMethodCallTranslatorPlugin
{
    public IEnumerable<IMethodCallTranslator> Translators => new IMethodCallTranslator[] { new SqlServerSemesterMethodCallTranslator() };
}

IRelationalTypeMappingSourcePlugin

public class SqlServerCustomTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
{
    public RelationalTypeMapping FindMapping(in RelationalTypeMappingInfo mappingInfo) 
        => mappingInfo.ClrType == typeof(Semester) || (mappingInfo.StoreTypeName == nameof(DateTime))
            ? new SqlServerSemesterTypeMapping(mappingInfo.StoreTypeName ?? "datetime")
            : null;
}

定义和注册翻译器后,必须在DbContext中对其进行配置。

IDbContextOptionsExtension

public class SqlServerCustomTypeOptionsExtension : IDbContextOptionsExtensionWithDebugInfo
{
    public string LogFragment => "using CustomTypes";

    public bool ApplyServices(IServiceCollection services)
    {
        services.AddEntityFrameworkSqlServerCustomTypes();

        return false;
    }

    public long GetServiceProviderHashCode() => 0;

    public void PopulateDebugInfo(IDictionary<string, string> debugInfo) 
        => debugInfo["SqlServer:" + nameof(SqlServerCustomDbContextOptionsBuilderExtensions.UseCustomTypes)] = "1";

    public void Validate(IDbContextOptions options)
    {
    }
}

扩展方法

public static class SqlServerCustomDbContextOptionsBuilderExtensions
{
    public static object UseCustomTypes(this SqlServerDbContextOptionsBuilder optionsBuilder)
    {
        if (optionsBuilder == null) throw new ArgumentNullException(nameof(optionsBuilder));

        // Registere die SqlServerDiamantOptionsExtension.
        var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder;

        var extension = coreOptionsBuilder.Options.FindExtension<SqlServerCustomTypeOptionsExtension>()
            ?? new SqlServerCustomTypeOptionsExtension();

        ((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension);

        // Configure Warnings
        coreOptionsBuilder
            .ConfigureWarnings(warnings => warnings
                .Log(RelationalEventId.QueryClientEvaluationWarning)        // Should be thrown to prevent only warnings if a query is not fully evaluated on the db
                .Ignore(RelationalEventId.ValueConversionSqlLiteralWarning));  // Ignore warnings for types that are using a ValueConverter

        return optionsBuilder;
    }
}


 public static class SqlServerServiceCollectionExtensions
{
    public static IServiceCollection AddEntityFrameworkSqlServerCustomTypes(
        this IServiceCollection serviceCollection)
    {
        if (serviceCollection == null) throw new ArgumentNullException(nameof(serviceCollection));

        new EntityFrameworkRelationalServicesBuilder(serviceCollection)
            .TryAddProviderSpecificServices(
                x => x.TryAddSingletonEnumerable<IRelationalTypeMappingSourcePlugin, SqlServerCustomTypeMappingSourcePlugin>()
                      .TryAddSingletonEnumerable<IMemberTranslatorPlugin, SqlServerCustomTypeMemberTranslatorPlugin>()
                      .TryAddSingletonEnumerable<IMethodCallTranslatorPlugin, SqlServerCustomTypeMethodCallTranslatorPlugin>());

        return serviceCollection;
    }
}

在DbContext中注册选项

dbOptionsBuilder.UseSqlServer(connectionString, builder => builder.UseCustomTypes())