Skip to content

Commit

Permalink
DBFunctions - Add support for instance methods on DbContext
Browse files Browse the repository at this point in the history
  • Loading branch information
pmiddleton committed Sep 11, 2017
1 parent c1bf9cc commit c4fbf7b
Show file tree
Hide file tree
Showing 6 changed files with 887 additions and 113 deletions.
5 changes: 0 additions & 5 deletions src/EFCore.Relational/Metadata/Internal/DbFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ private DbFunction(
throw new ArgumentException(RelationalStrings.DbFunctionGenericMethodNotSupported(methodInfo.DisplayName()));
}

if (!methodInfo.IsStatic)
{
throw new ArgumentException(RelationalStrings.DbFunctionMethodMustBeStatic(methodInfo.DisplayName()));
}

if (methodInfo.ReturnType == null
|| methodInfo.ReturnType == typeof(void))
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,6 @@
<data name="DbFunctionInvalidParameterType" xml:space="preserve">
<value>The parameter '{parameter}' for the DbFunction '{function}' has an invalid type '{type}'. Ensure the parameter type can be mapped by the current provider.</value>
</data>
<data name="DbFunctionMethodMustBeStatic" xml:space="preserve">
<value>The DbFunction '{function}' must be a static method. Non-static methods are not supported.</value>
</data>
<data name="DbFunctionGenericMethodNotSupported" xml:space="preserve">
<value>The DbFunction '{function}' is generic. Generic methods are not supported.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,9 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
{
Check.NotNull(methodCallExpression, nameof(methodCallExpression));

var operand = Visit(methodCallExpression.Object);
var operand = methodCallExpression.Object?.Type.IsSubclassOf(typeof(DbContext)) == true
? methodCallExpression.Object
: Visit(methodCallExpression.Object);

if (operand != null
|| methodCallExpression.Object == null)
Expand Down
14 changes: 0 additions & 14 deletions test/EFCore.Relational.Tests/Metadata/DbFunctionMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,6 @@ public static int MethodH<T>(T a, string b)
}
}

[Fact]
public virtual void Detects_non_static_function_on_dbcontext()
{
var modelBuilder = GetModelBuilder();

var methodInfo
= typeof(MyDerivedContext)
.GetRuntimeMethod(nameof(MyDerivedContext.NonStatic), new Type[] { });

Assert.Equal(
RelationalStrings.DbFunctionMethodMustBeStatic("MyDerivedContext.NonStatic"),
Assert.Throws<ArgumentException>(() => modelBuilder.HasDbFunction(methodInfo)).Message);
}

[Fact]
public void Detects_void_return_throws()
{
Expand Down
Loading

0 comments on commit c4fbf7b

Please sign in to comment.