Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for owned types mapped to same table as well as different tables #26706

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static EntityTypeBuilder ToTable(
{
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new TableBuilder(null, null, entityTypeBuilder.Metadata));
buildAction(new TableBuilder(null, null, entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static readonly IDictionary<Type, ServiceCharacteristics> RelationalServi
{ typeof(ISqlExpressionFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalQueryStringFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalParameterBasedSqlProcessorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalSharedTypeEntityExpansionHelper), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
Expand Down Expand Up @@ -190,6 +191,7 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<IRelationalParameterBasedSqlProcessorFactory, RelationalParameterBasedSqlProcessorFactory>();
TryAdd<IRelationalQueryStringFactory, RelationalQueryStringFactory>();
TryAdd<IQueryCompilationContextFactory, RelationalQueryCompilationContextFactory>();
TryAdd<IRelationalSharedTypeEntityExpansionHelper, RelationalSharedTypeEntityExpansionHelper>();

ServiceCollectionMap.GetInfrastructure()
.AddDependencySingleton<RelationalSqlGenerationHelperDependencies>()
Expand Down Expand Up @@ -225,7 +227,8 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencyScoped<RelationalConnectionDependencies>()
.AddDependencyScoped<RelationalDatabaseDependencies>()
.AddDependencyScoped<RelationalQueryContextDependencies>()
.AddDependencyScoped<RelationalQueryCompilationContextDependencies>();
.AddDependencyScoped<RelationalQueryCompilationContextDependencies>()
.AddDependencyScoped<RelationalSharedTypeEntityExpansionHelperDependencies>();

return base.TryAddCoreServices();
}
Expand Down
14 changes: 12 additions & 2 deletions src/EFCore.Relational/Metadata/Builders/TableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@ public class TableBuilder
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public TableBuilder(string? name, string? schema, IMutableEntityType entityType)
public TableBuilder(string? name, string? schema, EntityTypeBuilder entityTypeBuilder)
{
Metadata = entityType;
EntityTypeBuilder = entityTypeBuilder;
Metadata = entityTypeBuilder.Metadata;
}
//[EntityFrameworkInternal]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

//public TableBuilder(string? name, string? schema, IMutableEntityType entityType)
//{
// Metadata = entityType;
//}

/// <summary>
/// The entity type being configured.
/// </summary>
public virtual IMutableEntityType Metadata { get; }

/// <summary>
/// The entity type builder.
/// </summary>
public virtual EntityTypeBuilder EntityTypeBuilder { get; }
/// <summary>
/// Configures the table to be ignored by migrations.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// Service which helps with various aspects of shared type entity expansion extensibility for relational providrers.
/// </summary>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped" />. This means that each
/// <see cref="DbContext" /> instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-providers">Implementation of database providers and extensions</see>
/// and <see href="https://aka.ms/efcore-how-queries-work">How EF Core queries work</see> for more information.
/// </remarks>
public interface IRelationalSharedTypeEntityExpansionHelper
{
/// <summary>
/// Creates a SelectExpression representing owned type.
/// </summary>
public SelectExpression CreateInnerSelectExpression(
TableExpressionBase sourceTable,
IEntityType targetEntityType);

/// <summary>
/// Returns true if the given table expression matches table metadata, false otherwise.
/// </summary>
public bool TableMatchesMetadata(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The moment we needed to add this, we faulted our own design.

Copy link
Contributor Author

@maumar maumar Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, ideally there would be an abstract class for table-like sources (like "tableExpressionBase" and what currently is teb should be named something else?) from which TableExpression and temporalTable expression would inherit. Alternatively TemporalTableExpression should inherit from TableExpression rather than TableExpressionBase, but thats a smell also, since TemporalTableExpression is abstract and TableExpression is not :(

TableExpressionBase tableExpression,
ITableBase tableMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public RelationalQueryableMethodTranslatingExpressionVisitor(
_queryCompilationContext = queryCompilationContext;
_sqlTranslator = relationalDependencies.RelationalSqlTranslatingExpressionVisitorFactory.Create(queryCompilationContext, this);
_sharedTypeEntityExpandingExpressionVisitor =
new SharedTypeEntityExpandingExpressionVisitor(_sqlTranslator, sqlExpressionFactory);
new SharedTypeEntityExpandingExpressionVisitor(
_sqlTranslator,
sqlExpressionFactory,
relationalDependencies.RelationalSharedTypeEntityExpansionHelper);
_projectionBindingExpressionVisitor = new RelationalProjectionBindingExpressionVisitor(this, _sqlTranslator);
_sqlExpressionFactory = sqlExpressionFactory;
_subquery = false;
Expand All @@ -70,7 +73,11 @@ protected RelationalQueryableMethodTranslatingExpressionVisitor(
_sqlTranslator = RelationalDependencies.RelationalSqlTranslatingExpressionVisitorFactory.Create(
parentVisitor._queryCompilationContext, parentVisitor);
_sharedTypeEntityExpandingExpressionVisitor =
new SharedTypeEntityExpandingExpressionVisitor(_sqlTranslator, parentVisitor._sqlExpressionFactory);
new SharedTypeEntityExpandingExpressionVisitor(
_sqlTranslator,
parentVisitor._sqlExpressionFactory,
RelationalDependencies.RelationalSharedTypeEntityExpansionHelper);

_projectionBindingExpressionVisitor = new RelationalProjectionBindingExpressionVisitor(this, _sqlTranslator);
_sqlExpressionFactory = parentVisitor._sqlExpressionFactory;
_subquery = true;
Expand Down Expand Up @@ -997,15 +1004,18 @@ private static readonly MethodInfo _objectEqualsMethodInfo

private readonly RelationalSqlTranslatingExpressionVisitor _sqlTranslator;
private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalSharedTypeEntityExpansionHelper _sharedTypeEntityExpansionHelper;

private SelectExpression _selectExpression;

public SharedTypeEntityExpandingExpressionVisitor(
RelationalSqlTranslatingExpressionVisitor sqlTranslator,
ISqlExpressionFactory sqlExpressionFactory)
ISqlExpressionFactory sqlExpressionFactory,
IRelationalSharedTypeEntityExpansionHelper sharedTypeEntityExpansionHelper)
{
_sqlTranslator = sqlTranslator;
_sqlExpressionFactory = sqlExpressionFactory;
_sharedTypeEntityExpansionHelper = sharedTypeEntityExpansionHelper;
_selectExpression = null!;
}

Expand Down Expand Up @@ -1087,11 +1097,21 @@ protected override Expression VisitExtension(Expression extensionExpression)
return null;
}

var entityProjectionExpression = (EntityProjectionExpression)
(entityShaperExpression.ValueBufferExpression is ProjectionBindingExpression projectionBindingExpression
? _selectExpression.GetProjection(projectionBindingExpression)
: entityShaperExpression.ValueBufferExpression);

var foreignKey = navigation.ForeignKey;
if (navigation.IsCollection)
{
var innerShapedQuery = CreateShapedQueryExpression(
targetEntityType, _sqlExpressionFactory.Select(targetEntityType));
var innerSelectExpression = BuildInnerSelectExpressionForOwnedTypeMappedToDifferentTable(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smitpatel some refactoring here compared to 6.0 version, which didn't contain the logic for collection (which is also mapped to different table, the logic is pretty much the same) so I DRY'd it a bit

entityProjectionExpression,
navigation,
foreignKey,
targetEntityType);

var innerShapedQuery = CreateShapedQueryExpression(targetEntityType, innerSelectExpression);

var makeNullable = foreignKey.PrincipalKey.Properties
.Concat(foreignKey.Properties)
Expand Down Expand Up @@ -1139,11 +1159,6 @@ outerKey is NewArrayExpression newArrayExpression
Expression.Quote(correlationPredicate));
}

var entityProjectionExpression = (EntityProjectionExpression)
(entityShaperExpression.ValueBufferExpression is ProjectionBindingExpression projectionBindingExpression
? _selectExpression.GetProjection(projectionBindingExpression)
: entityShaperExpression.ValueBufferExpression);

var innerShaper = entityProjectionExpression.BindNavigation(navigation);
if (innerShaper == null)
{
Expand Down Expand Up @@ -1171,7 +1186,12 @@ outerKey is NewArrayExpression newArrayExpression
&& navigation.DeclaringEntityType.IsStrictlyDerivedFrom(entityShaperExpression.EntityType));

var entityProjection = _selectExpression.GenerateWeakEntityProjectionExpression(
targetEntityType, table, identifyingColumn.Name, identifyingColumn.Table, principalNullable);
targetEntityType,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no weak entity types anymore

table,
identifyingColumn.Name,
identifyingColumn.Table,
_sharedTypeEntityExpansionHelper,
principalNullable);

if (entityProjection != null)
{
Expand All @@ -1186,7 +1206,13 @@ outerKey is NewArrayExpression newArrayExpression
// Owned types don't support inheritance See https://github.com/dotnet/efcore/issues/9630
// So there is no handling for dependent having TPT
table = targetEntityType.GetViewOrTableMappings().Single().Table;
var innerSelectExpression = _sqlExpressionFactory.Select(targetEntityType);

var innerSelectExpression = BuildInnerSelectExpressionForOwnedTypeMappedToDifferentTable(
entityProjectionExpression,
navigation,
foreignKey,
targetEntityType);

var innerShapedQuery = CreateShapedQueryExpression(targetEntityType, innerSelectExpression);

var makeNullable = foreignKey.PrincipalKey.Properties
Expand All @@ -1212,14 +1238,60 @@ outerKey is NewArrayExpression newArrayExpression
innerShaper = new RelationalEntityShaperExpression(
targetEntityType,
_selectExpression.GenerateWeakEntityProjectionExpression(
targetEntityType, table, null, leftJoinTable, nullable: true)!,
targetEntityType,
table,
null,
leftJoinTable,
_sharedTypeEntityExpansionHelper,
nullable: true)!,
nullable: true);
}

entityProjectionExpression.AddNavigationBinding(navigation, innerShaper);
}

return innerShaper;

SelectExpression BuildInnerSelectExpressionForOwnedTypeMappedToDifferentTable(
EntityProjectionExpression entityProjectionExpression,
INavigation navigation,
IForeignKey foreignKey,
IEntityType targetEntityType)
{
// just need any column - we use it only to extract the table it originated from
var sourceColumn = entityProjectionExpression
.BindProperty(
navigation.IsOnDependent
? foreignKey.Properties[0]
: foreignKey.PrincipalKey.Properties[0]);

var sourceTable = FindRootTableExpressionForColumn(sourceColumn.Table, sourceColumn.Name);

return _sharedTypeEntityExpansionHelper.CreateInnerSelectExpression(
sourceTable,
targetEntityType);
}

static TableExpressionBase FindRootTableExpressionForColumn(TableExpressionBase table, string columnName)
{
if (table is JoinExpressionBase joinExpressionBase)
{
table = joinExpressionBase.Table;
}
else if (table is SetOperationBase setOperationBase)
{
table = setOperationBase.Source1;
}

if (table is SelectExpression selectExpression)
{
var matchingProjection = (ColumnExpression)selectExpression.Projection.Where(p => p.Alias == columnName).Single().Expression;

return FindRootTableExpressionForColumn(matchingProjection.Table, matchingProjection.Name);
}

return table;
}
}

private static Expression AddConvertToObject(Expression expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public sealed record RelationalQueryableMethodTranslatingExpressionVisitorDepend
[EntityFrameworkInternal]
public RelationalQueryableMethodTranslatingExpressionVisitorDependencies(
IRelationalSqlTranslatingExpressionVisitorFactory relationalSqlTranslatingExpressionVisitorFactory,
ISqlExpressionFactory sqlExpressionFactory)
ISqlExpressionFactory sqlExpressionFactory,
IRelationalSharedTypeEntityExpansionHelper relationalSharedTypeEntityExpansionHelper)
{
RelationalSqlTranslatingExpressionVisitorFactory = relationalSqlTranslatingExpressionVisitorFactory;
SqlExpressionFactory = sqlExpressionFactory;
RelationalSharedTypeEntityExpansionHelper = relationalSharedTypeEntityExpansionHelper;
}

/// <summary>
Expand All @@ -65,5 +67,10 @@ public RelationalQueryableMethodTranslatingExpressionVisitorDependencies(
/// The SQL expression factory.
/// </summary>
public ISqlExpressionFactory SqlExpressionFactory { get; init; }

/// <summary>
/// Shared type entity expansion helper.
/// </summary>
public IRelationalSharedTypeEntityExpansionHelper RelationalSharedTypeEntityExpansionHelper { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace Microsoft.EntityFrameworkCore.Query
{
/// <inheritdoc/>
public class RelationalSharedTypeEntityExpansionHelper : IRelationalSharedTypeEntityExpansionHelper
{
/// <summary>
/// Creates a new instance of the <see cref="RelationalSharedTypeEntityExpansionHelper" /> class.
/// </summary>
/// <param name="dependencies">Dependencies for this service.</param>
public RelationalSharedTypeEntityExpansionHelper(RelationalSharedTypeEntityExpansionHelperDependencies dependencies)
{
Dependencies = dependencies;
}

/// <summary>
/// Dependencies for this service.
/// </summary>
protected virtual RelationalSharedTypeEntityExpansionHelperDependencies Dependencies { get; }

/// <inheritdoc/>
public virtual SelectExpression CreateInnerSelectExpression(
TableExpressionBase sourceTable,
IEntityType targetEntityType)
=> Dependencies.SqlExpressionFactory.Select(targetEntityType);

/// <inheritdoc/>
public virtual bool TableMatchesMetadata(TableExpressionBase tableExpression, ITableBase tableMetadata)
=> tableExpression is TableExpression table
&& table.Name == tableMetadata.Name
&& table.Schema == tableMetadata.Schema;
}
}
Loading