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

Add complex type mappings to the default relational mappings #32867

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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
106 changes: 79 additions & 27 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data;
using System.Text;
using System.Text.Json;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand Down Expand Up @@ -323,32 +323,7 @@ private static void AddDefaultMappings(
}
else
{
foreach (var property in entityType.GetProperties())
{
var columnName = property.IsPrimaryKey() || isTpc || isTph || property.DeclaringType == mappedType
? property.GetColumnName()
: null;
if (columnName == null)
{
continue;
}

var column = (ColumnBase<ColumnMappingBase>?)defaultTable.FindColumn(columnName);
if (column == null)
{
column = new ColumnBase<ColumnMappingBase>(columnName, property.GetColumnType(), defaultTable)
{
IsNullable = property.IsColumnNullable()
};
defaultTable.Columns.Add(columnName, column);
}
else if (!property.IsColumnNullable())
{
column.IsNullable = false;
}

CreateColumnMapping(column, property, tableMapping);
}
CreateDefaultColumnMapping(entityType, mappedType, defaultTable, tableMapping, isTph, isTpc);
}

if (((ITableMappingBase)tableMapping).ColumnMappings.Any()
Expand All @@ -369,6 +344,83 @@ private static void AddDefaultMappings(
tableMappings.Reverse();
}

private static void CreateDefaultColumnMapping(
ITypeBase typeBase,
ITypeBase mappedType,
TableBase defaultTable,
TableMappingBase<ColumnMappingBase> tableMapping,
bool isTph,
bool isTpc)
{
foreach (var property in typeBase.GetProperties())
{
var columnName = property.IsPrimaryKey() || isTpc || isTph || property.DeclaringType == mappedType
? GetColumnName(property)
: null;

if (columnName == null)
{
continue;
}

var column = (ColumnBase<ColumnMappingBase>?)defaultTable.FindColumn(columnName);
if (column == null)
{
column = new ColumnBase<ColumnMappingBase>(columnName, property.GetColumnType(), defaultTable)
{
IsNullable = property.IsColumnNullable()
};
defaultTable.Columns.Add(columnName, column);
}
else if (!property.IsColumnNullable())
{
column.IsNullable = false;
}

CreateColumnMapping(column, property, tableMapping);
}

foreach (var complexProperty in typeBase.GetDeclaredComplexProperties())
{
var complexType = complexProperty.ComplexType;
tableMapping = new TableMappingBase<ColumnMappingBase>(complexType, defaultTable, includesDerivedTypes: null);

CreateDefaultColumnMapping(complexType, complexType, defaultTable, tableMapping, isTph, isTpc);

var tableMappings = (List<TableMappingBase<ColumnMappingBase>>?)complexType
.FindRuntimeAnnotationValue(RelationalAnnotationNames.DefaultMappings);
if (tableMappings == null)
{
tableMappings = new List<TableMappingBase<ColumnMappingBase>>();
complexType.AddRuntimeAnnotation(RelationalAnnotationNames.DefaultMappings, tableMappings);
}
tableMappings.Add(tableMapping);

defaultTable.ComplexTypeMappings.Add(tableMapping);
}

static string GetColumnName(IProperty property)
AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved
{
var complexType = property.DeclaringType as IComplexType;
if (complexType != null)
{
var builder = new StringBuilder();
builder.Append(property.Name);
while (complexType != null)
{
builder.Insert(0, "_");
builder.Insert(0, complexType.ComplexProperty.Name);

complexType = complexType.ComplexProperty.DeclaringType as IComplexType;
}

return builder.ToString();
}

return property.GetColumnName();
}
}

private static void AddTables(
RelationalModel databaseModel,
IEntityType entityType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2563,9 +2563,13 @@ public static StructuralTypeShaperExpression GenerateComplexPropertyShaperExpres
var propertyExpressionMap = new Dictionary<IProperty, ColumnExpression>();

// We do not support complex type splitting, so we will only ever have a single table/view mapping to it.
// See Issue #32853 and Issue #31248
var complexTypeTable = complexProperty.ComplexType.GetViewOrTableMappings().Single().Table;
var tableReferenceExpression = containerProjection.TableMap[complexTypeTable];

if (!containerProjection.TableMap.TryGetValue(complexTypeTable, out var tableReferenceExpression))
ajcvickers marked this conversation as resolved.
Show resolved Hide resolved
{
complexTypeTable = complexProperty.ComplexType.GetDefaultMappings().Single().Table;
tableReferenceExpression = containerProjection.TableMap[complexTypeTable];
}
var isComplexTypeNullable = containerProjection.IsNullable || complexProperty.IsNullable;

// If the complex property is declared on a type that's derived relative to the type being projected, the projected column is
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.TestModels.ComplexTypeModel;

namespace Microsoft.EntityFrameworkCore.Query;

public class ComplexTypeQuerySqlServerTest : ComplexTypeQueryRelationalTestBase<
Expand Down Expand Up @@ -742,6 +744,121 @@ public override async Task Union_two_different_struct_complex_type(bool async)
AssertSql();
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Filter_on_property_inside_complex_type_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
"""
SELECT [c].[Id], [c].[Name], [c].[BillingAddress_AddressLine1], [c].[BillingAddress_AddressLine2], [c].[BillingAddress_Tags], [c].[BillingAddress_ZipCode], [c].[BillingAddress_Country_Code], [c].[BillingAddress_Country_FullName], [c].[ShippingAddress_AddressLine1], [c].[ShippingAddress_AddressLine2], [c].[ShippingAddress_Tags], [c].[ShippingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
WHERE [c].[ShippingAddress_ZipCode] = 7728
"""),
ss => ss.Set<Customer>().Where(c => c.ShippingAddress.ZipCode == 07728));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Filter_on_property_inside_complex_type_after_subquery_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSql(
$"""
SELECT DISTINCT [t].[Id], [t].[Name], [t].[BillingAddress_AddressLine1], [t].[BillingAddress_AddressLine2], [t].[BillingAddress_Tags], [t].[BillingAddress_ZipCode], [t].[BillingAddress_Country_Code], [t].[BillingAddress_Country_FullName], [t].[ShippingAddress_AddressLine1], [t].[ShippingAddress_AddressLine2], [t].[ShippingAddress_Tags], [t].[ShippingAddress_ZipCode], [t].[ShippingAddress_Country_Code], [t].[ShippingAddress_Country_FullName]
FROM (
SELECT [c].[Id], [c].[Name], [c].[BillingAddress_AddressLine1], [c].[BillingAddress_AddressLine2], [c].[BillingAddress_Tags], [c].[BillingAddress_ZipCode], [c].[BillingAddress_Country_Code], [c].[BillingAddress_Country_FullName], [c].[ShippingAddress_AddressLine1], [c].[ShippingAddress_AddressLine2], [c].[ShippingAddress_Tags], [c].[ShippingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
ORDER BY [c].[Id]
OFFSET {1} ROWS
) AS [t]
WHERE [t].[ShippingAddress_ZipCode] = 7728
"""),
ss => ss.Set<Customer>()
.OrderBy(c => c.Id)
.Skip(1)
.Distinct()
.Where(c => c.ShippingAddress.ZipCode == 07728));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Load_complex_type_after_subquery_on_entity_type_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSql(
$"""
SELECT DISTINCT [t].[Id], [t].[Name], [t].[BillingAddress_AddressLine1], [t].[BillingAddress_AddressLine2], [t].[BillingAddress_Tags], [t].[BillingAddress_ZipCode], [t].[BillingAddress_Country_Code], [t].[BillingAddress_Country_FullName], [t].[ShippingAddress_AddressLine1], [t].[ShippingAddress_AddressLine2], [t].[ShippingAddress_Tags], [t].[ShippingAddress_ZipCode], [t].[ShippingAddress_Country_Code], [t].[ShippingAddress_Country_FullName]
FROM (
SELECT [c].[Id], [c].[Name], [c].[BillingAddress_AddressLine1], [c].[BillingAddress_AddressLine2], [c].[BillingAddress_Tags], [c].[BillingAddress_ZipCode], [c].[BillingAddress_Country_Code], [c].[BillingAddress_Country_FullName], [c].[ShippingAddress_AddressLine1], [c].[ShippingAddress_AddressLine2], [c].[ShippingAddress_Tags], [c].[ShippingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
ORDER BY [c].[Id]
OFFSET {1} ROWS
) AS [t]
"""),
ss => ss.Set<Customer>()
.OrderBy(c => c.Id)
.Skip(1)
.Distinct());

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_complex_type_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
"""
SELECT [c].[ShippingAddress_AddressLine1], [c].[ShippingAddress_AddressLine2], [c].[ShippingAddress_Tags], [c].[ShippingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
""").Select(c => c.ShippingAddress),
ss => ss.Set<Customer>().Select(c => c.ShippingAddress));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_nested_complex_type_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
"""
SELECT [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
""").Select(c => c.ShippingAddress.Country),
ss => ss.Set<Customer>().Select(c => c.ShippingAddress.Country));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_single_property_on_nested_complex_type_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
"""
SELECT [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
""").Select(c => c.ShippingAddress.Country.FullName),
ss => ss.Set<Customer>().Select(c => c.ShippingAddress.Country.FullName));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_complex_type_Where_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
"""
SELECT [c].[ShippingAddress_AddressLine1], [c].[ShippingAddress_AddressLine2], [c].[ShippingAddress_Tags], [c].[ShippingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
""").Select(c => c.ShippingAddress).Where(a => a.ZipCode == 07728),
ss => ss.Set<Customer>().Select(c => c.ShippingAddress).Where(a => a.ZipCode == 07728));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_complex_type_Distinct_with_FromSql(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
"""
SELECT [c].[ShippingAddress_AddressLine1], [c].[ShippingAddress_AddressLine2], [c].[ShippingAddress_Tags], [c].[ShippingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
""").Select(c => c.ShippingAddress).Distinct(),
ss => ss.Set<Customer>().Select(c => c.ShippingAddress).Distinct());

[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
Expand Down
Loading
Loading