Skip to content

Commit

Permalink
Metadata: Bring back SqlServerDbFunctionConvention (#21388)
Browse files Browse the repository at this point in the history
Resolve #20182
  • Loading branch information
smitpatel authored Jun 23, 2020
1 parent 9d9c25e commit 2213f95
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public override ConventionSet CreateConventionSet()
ReplaceConvention(conventionSet.ModelFinalizingConventions, storeGenerationConvention);
ReplaceConvention(conventionSet.ModelFinalizingConventions,
(SharedTableConvention)new SqlServerSharedTableConvention(Dependencies, RelationalDependencies));
conventionSet.ModelFinalizingConventions.Add(new SqlServerDbFunctionConvention(Dependencies, RelationalDependencies));

return conventionSet;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
/// <summary>
/// A convention that ensures that <see cref="IDbFunction.Schema"/> is populated for database functions which are not built-in.
/// </summary>
public class SqlServerDbFunctionConvention : IModelFinalizingConvention
{
/// <summary>
/// Creates a new instance of <see cref="SqlServerDbFunctionConvention" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param>
/// <param name="relationalDependencies"> Parameter object containing relational dependencies for this convention. </param>
public SqlServerDbFunctionConvention(
[NotNull] ProviderConventionSetBuilderDependencies dependencies,
[NotNull] RelationalConventionSetBuilderDependencies relationalDependencies)
{
Check.NotNull(dependencies, nameof(dependencies));
Check.NotNull(relationalDependencies, nameof(relationalDependencies));

Dependencies = dependencies;
}

/// <summary>
/// Parameter object containing service dependencies.
/// </summary>
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; }

/// <inheritdoc />
public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
foreach (var dbFunction in modelBuilder.Metadata.GetDbFunctions())
{
if (string.IsNullOrEmpty(dbFunction.Schema))
{
dbFunction.SetSchema("dbo");
}
}
}
}
}
55 changes: 0 additions & 55 deletions src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,60 +99,5 @@ protected override void GenerateLimitOffset(SelectExpression selectExpression)
}
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlFunction(SqlFunctionExpression sqlFunctionExpression)
{
Check.NotNull(sqlFunctionExpression, nameof(sqlFunctionExpression));

if (!sqlFunctionExpression.IsBuiltIn
&& string.IsNullOrEmpty(sqlFunctionExpression.Schema))
{
sqlFunctionExpression = sqlFunctionExpression.IsNiladic
? new SqlFunctionExpression(
schema: "dbo",
sqlFunctionExpression.Name,
sqlFunctionExpression.IsNullable,
sqlFunctionExpression.Type,
sqlFunctionExpression.TypeMapping)
: new SqlFunctionExpression(
schema: "dbo",
sqlFunctionExpression.Name,
sqlFunctionExpression.Arguments,
sqlFunctionExpression.IsNullable,
sqlFunctionExpression.ArgumentsPropagateNullability,
sqlFunctionExpression.Type,
sqlFunctionExpression.TypeMapping);
}

return base.VisitSqlFunction(sqlFunctionExpression);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitTableValuedFunction(TableValuedFunctionExpression tableValuedFunctionExpression)
{
Check.NotNull(tableValuedFunctionExpression, nameof(tableValuedFunctionExpression));

if (string.IsNullOrEmpty(tableValuedFunctionExpression.Schema))
{
tableValuedFunctionExpression = new TableValuedFunctionExpression(
tableValuedFunctionExpression.Alias,
schema: "dbo",
tableValuedFunctionExpression.Name,
tableValuedFunctionExpression.Arguments);
}

return base.VisitTableValuedFunction(tableValuedFunctionExpression);
}
}
}

0 comments on commit 2213f95

Please sign in to comment.