diff --git a/src/EFCore.Jet/Extensions/JetDbFunctionsExtensions.cs b/src/EFCore.Jet/Extensions/JetDbFunctionsExtensions.cs index 15f386a..87768da 100644 --- a/src/EFCore.Jet/Extensions/JetDbFunctionsExtensions.cs +++ b/src/EFCore.Jet/Extensions/JetDbFunctionsExtensions.cs @@ -444,5 +444,9 @@ namespace Microsoft.EntityFrameworkCore [CanBeNull] this DbFunctions _, [NotNull] string expression) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(IsDate))); + + public static double Random( + [CanBeNull] this DbFunctions _) + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Random))); } } diff --git a/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetMethodCallTranslatorProvider.cs b/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetMethodCallTranslatorProvider.cs index 436261f..ab36260 100644 --- a/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetMethodCallTranslatorProvider.cs +++ b/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetMethodCallTranslatorProvider.cs @@ -25,6 +25,7 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal AddTranslators( new IMethodCallTranslator[] { + new JetRandomTranslator(sqlExpressionFactory), new JetDateTimeMethodTranslator(sqlExpressionFactory), new JetDateDiffFunctionsTranslator(sqlExpressionFactory), new JetIsDateFunctionTranslator(sqlExpressionFactory), diff --git a/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetRandomTranslator.cs b/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetRandomTranslator.cs new file mode 100644 index 0000000..e818931 --- /dev/null +++ b/src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetRandomTranslator.cs @@ -0,0 +1,48 @@ +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; + +namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal +{ + /// + /// This API supports the Entity Framework Core infrastructure and is not intended to be used + /// directly from your code. This API may change or be removed in future releases. + /// + public class JetRandomTranslator : IMethodCallTranslator + { + private static readonly MethodInfo[] _methodInfo = + { + typeof(DbFunctionsExtensions).GetRuntimeMethod(nameof(DbFunctionsExtensions.Random), new[] + { + typeof(DbFunctions) + }), + typeof(JetDbFunctionsExtensions).GetRuntimeMethod(nameof(JetDbFunctionsExtensions.Random), new[] + { + typeof(DbFunctions) + }) + }; + private readonly JetSqlExpressionFactory _sqlExpressionFactory; + + public JetRandomTranslator(ISqlExpressionFactory sqlExpressionFactory) + => _sqlExpressionFactory = (JetSqlExpressionFactory)sqlExpressionFactory; + + public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList arguments, IDiagnosticsLogger logger) + { + return _methodInfo.Contains(method) + ? _sqlExpressionFactory.Function( + "Rnd", + Array.Empty(), + false, + Enumerable.Empty(), + method.ReturnType) + : null; + } + } +} \ No newline at end of file