-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default Sql Server db functions to DBO schema
- Loading branch information
1 parent
0a9cc23
commit dc65e9c
Showing
5 changed files
with
223 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/EFCore.SqlServer/Metadata/Conventions/Internal/SqlServerDbFunctionConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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 System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal | ||
{ | ||
public class SqlServerDbFunctionConvention : IModelBuiltConvention | ||
{ | ||
public virtual InternalModelBuilder Apply(InternalModelBuilder modelBuilder) | ||
{ | ||
Check.NotNull(modelBuilder, nameof(modelBuilder)); | ||
|
||
if (!string.IsNullOrWhiteSpace(modelBuilder.Metadata.Relational().DefaultSchema)) | ||
return modelBuilder; | ||
|
||
foreach (var dbFunction in modelBuilder.Metadata.Relational().DbFunctions) | ||
{ | ||
if (dbFunction.Schema == null) | ||
modelBuilder.Metadata.Relational().GetOrAddDbFunction(dbFunction.MethodInfo).Schema = "dbo"; | ||
} | ||
|
||
return modelBuilder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
test/EFCore.SqlServer.FunctionalTests/SqlServerDbFunctionMetadataTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal ; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class SqlServerDbFunctionMetadataTests | ||
{ | ||
public class TestMethods | ||
{ | ||
public static int Foo() | ||
{ | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
public static MethodInfo MethodFoo = typeof(TestMethods).GetRuntimeMethod(nameof(TestMethods.Foo), new Type[] { }); | ||
|
||
|
||
[Fact] | ||
public virtual void DbFuction_defaults_schema_to_dbo() | ||
{ | ||
var modelBuilder = GetModelBuilder(); | ||
|
||
var dbFunction = modelBuilder.HasDbFunction(MethodFoo); | ||
|
||
((Model)modelBuilder.Model).Validate(); | ||
|
||
Assert.Equal("dbo", dbFunction.Metadata.Schema); | ||
} | ||
|
||
[Fact] | ||
public virtual void DbFuction_set_schmea_is_not_overridden() | ||
{ | ||
var modelBuilder = GetModelBuilder(); | ||
|
||
var dbFunction = modelBuilder.HasDbFunction(MethodFoo).HasSchema("abc"); | ||
|
||
((Model)modelBuilder.Model).Validate(); | ||
|
||
Assert.Equal("abc", dbFunction.Metadata.Schema); | ||
} | ||
|
||
[Fact] | ||
public virtual void DbFuction_dbo_not_set_if_default_schema_is_set() | ||
{ | ||
var modelBuilder = GetModelBuilder(); | ||
|
||
modelBuilder.HasDefaultSchema("qwerty"); | ||
|
||
var dbFunction = modelBuilder.HasDbFunction(MethodFoo); | ||
|
||
((Model)modelBuilder.Model).Validate(); | ||
|
||
Assert.Equal(null, dbFunction.Metadata.Schema); | ||
} | ||
|
||
private ModelBuilder GetModelBuilder() | ||
{ | ||
var conventionset = new ConventionSet(); | ||
|
||
conventionset.ModelBuiltConventions.Add(new SqlServerDbFunctionConvention()); | ||
|
||
return new ModelBuilder(conventionset); | ||
} | ||
} | ||
} |