Skip to content
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
128 changes: 108 additions & 20 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +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.Text;
using System.Text.Json;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand All @@ -13,6 +14,9 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
/// </summary>
public class RelationalModel : Annotatable, IRelationalModel
{
internal static readonly bool UseOldBehavior32699 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32699", out var enabled32699) && enabled32699;

private bool _isReadOnly;

/// <summary>
Expand Down Expand Up @@ -340,31 +344,38 @@ private static void AddDefaultMappings(
}
else
{
foreach (var property in entityType.GetProperties())
if (UseOldBehavior32699)
{
var columnName = property.IsPrimaryKey() || isTpc || isTph || property.DeclaringType == mappedType
? property.GetColumnName()
: null;
if (columnName == null)
foreach (var property in entityType.GetProperties())
{
continue;
}
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)
var column = (ColumnBase<ColumnMappingBase>?)defaultTable.FindColumn(columnName);
if (column == null)
{
IsNullable = property.IsColumnNullable()
};
defaultTable.Columns.Add(columnName, column);
}
else if (!property.IsColumnNullable())
{
column.IsNullable = false;
}
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);
CreateColumnMapping(column, property, tableMapping);
}
}
else
{
CreateDefaultColumnMapping(entityType, mappedType, defaultTable, tableMapping, isTph, isTpc);
}
}

Expand All @@ -386,6 +397,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: false);

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)
{
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
13 changes: 11 additions & 2 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3201,9 +3201,18 @@ 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];

TableReferenceExpression? tableReferenceExpression;
if (RelationalModel.UseOldBehavior32699)
{
tableReferenceExpression = containerProjection.TableMap[complexTypeTable];
}
else if (!containerProjection.TableMap.TryGetValue(complexTypeTable, out tableReferenceExpression))
{
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
Loading