Skip to content

Commit

Permalink
Merge in 'release/8.0' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnet-bot committed Jan 16, 2024
2 parents e6ea1d7 + 86dd535 commit 7c8f682
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Storage.Internal;
Expand Down Expand Up @@ -185,6 +186,7 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<IRelationalParameterBasedSqlProcessorFactory, RelationalParameterBasedSqlProcessorFactory>();
TryAdd<IRelationalQueryStringFactory, RelationalQueryStringFactory>();
TryAdd<IQueryCompilationContextFactory, RelationalQueryCompilationContextFactory>();
TryAdd<IAdHocMapper, RelationalAdHocMapper>();

ServiceCollectionMap.GetInfrastructure()
.AddDependencySingleton<RelationalSqlGenerationHelperDependencies>()
Expand Down
51 changes: 51 additions & 0 deletions src/EFCore.Relational/Metadata/Internal/RelationalAdHocMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;

/// <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>
#pragma warning disable EF1001 // AdHocMapper should be made public
public class RelationalAdHocMapper : AdHocMapper
{
private static readonly bool UseOldBehavior32680 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32680", out var enabled32680) && enabled32680;

/// <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>
public RelationalAdHocMapper(
IModel model,
ModelCreationDependencies modelCreationDependencies)
: base(model, modelCreationDependencies)
{
}

/// <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>
public override ConventionSet BuildConventionSet()
{
if (UseOldBehavior32680)
{
return base.BuildConventionSet();
}

var conventionSet = base.BuildConventionSet();
conventionSet.Remove(typeof(RelationalDbFunctionAttributeConvention));
conventionSet.Remove(typeof(TableNameFromDbSetConvention));
conventionSet.Remove(typeof(TableValuedDbFunctionConvention));
return conventionSet;
}
}
#pragma warning restore EF1001
71 changes: 37 additions & 34 deletions src/EFCore/Metadata/Internal/AdHocMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,47 @@ public class AdHocMapper : IAdHocMapper
_modelCreationDependencies = modelCreationDependencies;
}

private ConventionSet ConventionSet
/// <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>
public virtual ConventionSet BuildConventionSet()
{
get
{
if (_conventionSet == null)
{
_conventionSet = _modelCreationDependencies.ConventionSetBuilder.CreateConventionSet();
_conventionSet.Remove(typeof(DbSetFindingConvention));
_conventionSet.Remove(typeof(RelationshipDiscoveryConvention));
_conventionSet.Remove(typeof(KeyDiscoveryConvention));
_conventionSet.Remove(typeof(CascadeDeleteConvention));
_conventionSet.Remove(typeof(ChangeTrackingStrategyConvention));
_conventionSet.Remove(typeof(DeleteBehaviorAttributeConvention));
_conventionSet.Remove(typeof(ForeignKeyAttributeConvention));
_conventionSet.Remove(typeof(ForeignKeyIndexConvention));
_conventionSet.Remove(typeof(ForeignKeyPropertyDiscoveryConvention));
_conventionSet.Remove(typeof(IndexAttributeConvention));
_conventionSet.Remove(typeof(KeyAttributeConvention));
_conventionSet.Remove(typeof(KeylessAttributeConvention));
_conventionSet.Remove(typeof(ManyToManyJoinEntityTypeConvention));
_conventionSet.Remove(typeof(RequiredNavigationAttributeConvention));
_conventionSet.Remove(typeof(NavigationBackingFieldAttributeConvention));
_conventionSet.Remove(typeof(InversePropertyAttributeConvention));
_conventionSet.Remove(typeof(NavigationEagerLoadingConvention));
_conventionSet.Remove(typeof(NonNullableNavigationConvention));
_conventionSet.Remove(typeof(NotMappedTypeAttributeConvention));
_conventionSet.Remove(typeof(OwnedAttributeConvention));
_conventionSet.Remove(typeof(QueryFilterRewritingConvention));
_conventionSet.Remove(typeof(ServicePropertyDiscoveryConvention));
_conventionSet.Remove(typeof(ValueGenerationConvention));
_conventionSet.Remove(typeof(BaseTypeDiscoveryConvention));
_conventionSet.Remove(typeof(DiscriminatorConvention));
}
var conventionSet = _modelCreationDependencies.ConventionSetBuilder.CreateConventionSet();
conventionSet.Remove(typeof(DbSetFindingConvention));
conventionSet.Remove(typeof(RelationshipDiscoveryConvention));
conventionSet.Remove(typeof(KeyDiscoveryConvention));
conventionSet.Remove(typeof(CascadeDeleteConvention));
conventionSet.Remove(typeof(ChangeTrackingStrategyConvention));
conventionSet.Remove(typeof(DeleteBehaviorAttributeConvention));
conventionSet.Remove(typeof(ForeignKeyAttributeConvention));
conventionSet.Remove(typeof(ForeignKeyIndexConvention));
conventionSet.Remove(typeof(ForeignKeyPropertyDiscoveryConvention));
conventionSet.Remove(typeof(IndexAttributeConvention));
conventionSet.Remove(typeof(KeyAttributeConvention));
conventionSet.Remove(typeof(KeylessAttributeConvention));
conventionSet.Remove(typeof(ManyToManyJoinEntityTypeConvention));
conventionSet.Remove(typeof(RequiredNavigationAttributeConvention));
conventionSet.Remove(typeof(NavigationBackingFieldAttributeConvention));
conventionSet.Remove(typeof(InversePropertyAttributeConvention));
conventionSet.Remove(typeof(NavigationEagerLoadingConvention));
conventionSet.Remove(typeof(NonNullableNavigationConvention));
conventionSet.Remove(typeof(NotMappedTypeAttributeConvention));
conventionSet.Remove(typeof(OwnedAttributeConvention));
conventionSet.Remove(typeof(QueryFilterRewritingConvention));
conventionSet.Remove(typeof(ServicePropertyDiscoveryConvention));
conventionSet.Remove(typeof(ValueGenerationConvention));
conventionSet.Remove(typeof(BaseTypeDiscoveryConvention));
conventionSet.Remove(typeof(DiscriminatorConvention));

return _conventionSet;
}
return conventionSet;
}

private ConventionSet ConventionSet
=> (_conventionSet ??= BuildConventionSet());

/// <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
Expand Down
41 changes: 41 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/SqlServerEndToEndTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -216,6 +217,46 @@ private class ByteAdNum
public string Lucy { get; set; }
}

[ConditionalFact] // Issue #29931
public void Can_use_SqlQuery_when_context_has_DbFunction()
{
using var testDatabase = SqlServerTestStore.CreateInitialized(DatabaseName);
var options = Fixture.CreateOptions(testDatabase);
using (var context = new DbFunctionContext(options))
{
var result = context.Database
.SqlQueryRaw<RawResult>("SELECT Name from sys.databases")
.OrderBy(d => d.Name)
.ToList();
}
}

private class DbFunctionContext(DbContextOptions options) : DbContext(options)
{
[DbFunction("tvp", "dbo")]
public IQueryable<TvpResult> Tvp(int? storeid)
=> FromExpression(() => Tvp(storeid));

protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<TvpResult>().HasNoKey();
}

private class TvpResult
{
public int Id { get; set; }

[Required]
public string Name { get; set; }

[Column(TypeName = "decimal(18,2)")]
public decimal Total { get; set; }
}

private class RawResult
{
public string Name { get; set; }
}

[ConditionalFact]
public void Can_use_string_enum_or_byte_array_as_key()
{
Expand Down

0 comments on commit 7c8f682

Please sign in to comment.