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

Draft: Mapping a single property to multiple columns #24909

Closed
wants to merge 1 commit 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 @@ -64,7 +64,7 @@ public static string GetDefaultName(this IReadOnlyForeignKey foreignKey)
.Append('_')
.Append(principalTableName)
.Append('_')
.AppendJoin(foreignKey.Properties.Select(p => p.GetColumnBaseName()), "_")
.AppendJoin(foreignKey.Properties.Select(p => p.GetColumnBaseName())!, "_")
.ToString();

return Uniquifier.Truncate(name, foreignKey.DeclaringEntityType.Model.GetMaxIdentifierLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static string GetDefaultDatabaseName(this IReadOnlyIndex index)
.Append("IX_")
.Append(tableName)
.Append('_')
.AppendJoin(index.Properties.Select(p => p.GetColumnBaseName()), "_")
.AppendJoin(index.Properties.Select(p => p.GetColumnBaseName())!, "_")
.ToString();

return Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static string GetDefaultName(this IReadOnlyKey key)
.Append("AK_")
.Append(tableName)
.Append('_')
.AppendJoin(key.Properties.Select(p => p.GetColumnBaseName()), "_")
.AppendJoin(key.Properties.Select(p => p.GetColumnBaseName())!, "_")
.ToString();

return Uniquifier.Truncate(name, key.DeclaringEntityType.Model.GetMaxIdentifierLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static string GetColumnName(this IProperty property)
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The base name of the column to which the property would be mapped. </returns>
public static string GetColumnBaseName(this IReadOnlyProperty property)
public static string? GetColumnBaseName(this IReadOnlyProperty property)
=> (string?)property.FindAnnotation(RelationalAnnotationNames.ColumnName)?.Value ?? property.GetDefaultColumnBaseName();

/// <summary>
Expand Down
220 changes: 150 additions & 70 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,43 @@ private static void AddDefaultMappings(RelationalModel databaseModel, IEntityTyp
};

foreach (var property in entityType.GetProperties())
{
var pseudoProperties = property.GetPseudoProperties();
if (pseudoProperties.Any())
{
foreach (var pseudoProperty in pseudoProperties)
{
MakeColumnMapping(pseudoProperty);
}
}
else
{
MakeColumnMapping(property);
}
}

if (entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.DefaultMappings)
is not List<TableMappingBase> tableMappings)
{
tableMappings = new List<TableMappingBase>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.DefaultMappings, tableMappings);
}

if (tableMapping.ColumnMappings.Count != 0
|| tableMappings.Count == 0)
{
tableMappings.Add(tableMapping);
defaultTable.EntityTypeMappings.Add(tableMapping);
}

tableMappings.Reverse();

void MakeColumnMapping(IProperty property)
{
var columnName = property.GetColumnBaseName();
if (columnName == null)
{
continue;
return;
}

var column = (ColumnBase?)defaultTable.FindColumn(columnName);
Expand Down Expand Up @@ -293,22 +325,6 @@ private static void AddDefaultMappings(RelationalModel databaseModel, IEntityTyp

columnMappings.Add(columnMapping);
}

if (entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.DefaultMappings)
is not List<TableMappingBase> tableMappings)
{
tableMappings = new List<TableMappingBase>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.DefaultMappings, tableMappings);
}

if (tableMapping.ColumnMappings.Count != 0
|| tableMappings.Count == 0)
{
tableMappings.Add(tableMapping);
defaultTable.EntityTypeMappings.Add(tableMapping);
}

tableMappings.Reverse();
}

private static void AddTables(RelationalModel databaseModel, IEntityType entityType)
Expand Down Expand Up @@ -354,11 +370,44 @@ private static void AddTables(RelationalModel databaseModel, IEntityType entityT
IsSplitEntityTypePrincipal = true
};
foreach (var property in mappedType.GetProperties())
{
var pseudoProperties = property.GetPseudoProperties();
if (pseudoProperties.Any())
{
foreach (var pseudoProperty in pseudoProperties)
{
MakeColumnMapping(pseudoProperty);
}
}
else
{
MakeColumnMapping(property);
}
}

mappedType = mappedType.BaseType;

tableMappings = entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.TableMappings)
as List<TableMapping>;
if (tableMappings == null)
{
tableMappings = new List<TableMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.TableMappings, tableMappings);
}

if (tableMapping.ColumnMappings.Count != 0
|| tableMappings.Count == 0)
{
tableMappings.Add(tableMapping);
table.EntityTypeMappings.Add(tableMapping);
}

void MakeColumnMapping(IProperty property)
{
var columnName = property.GetColumnName(mappedTable);
if (columnName == null)
{
continue;
return;
}

var column = (Column?)table.FindColumn(columnName);
Expand Down Expand Up @@ -386,23 +435,6 @@ private static void AddTables(RelationalModel databaseModel, IEntityType entityT

columnMappings.Add(columnMapping);
}

mappedType = mappedType.BaseType;

tableMappings = entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.TableMappings)
as List<TableMapping>;
if (tableMappings == null)
{
tableMappings = new List<TableMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.TableMappings, tableMappings);
}

if (tableMapping.ColumnMappings.Count != 0
|| tableMappings.Count == 0)
{
tableMappings.Add(tableMapping);
table.EntityTypeMappings.Add(tableMapping);
}
}

tableMappings?.Reverse();
Expand Down Expand Up @@ -445,11 +477,43 @@ private static void AddViews(RelationalModel databaseModel, IEntityType entityTy
IsSplitEntityTypePrincipal = true
};
foreach (var property in mappedType.GetProperties())
{
var pseudoProperties = property.GetPseudoProperties();
if (pseudoProperties.Any())
{
foreach (var pseudoProperty in pseudoProperties)
{
MakeColumnMapping(pseudoProperty);
}
}
else
{
MakeColumnMapping(property);
}
}

mappedType = mappedType.BaseType;

viewMappings = entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.ViewMappings) as List<ViewMapping>;
if (viewMappings == null)
{
viewMappings = new List<ViewMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.ViewMappings, viewMappings);
}

if (viewMapping.ColumnMappings.Count != 0
|| viewMappings.Count == 0)
{
viewMappings.Add(viewMapping);
view.EntityTypeMappings.Add(viewMapping);
}

void MakeColumnMapping(IProperty property)
{
var columnName = property.GetColumnName(mappedView);
if (columnName == null)
{
continue;
return;
}

var column = (ViewColumn?)view.FindColumn(columnName);
Expand Down Expand Up @@ -477,22 +541,6 @@ private static void AddViews(RelationalModel databaseModel, IEntityType entityTy

columnMappings.Add(columnMapping);
}

mappedType = mappedType.BaseType;

viewMappings = entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.ViewMappings) as List<ViewMapping>;
if (viewMappings == null)
{
viewMappings = new List<ViewMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.ViewMappings, viewMappings);
}

if (viewMapping.ColumnMappings.Count != 0
|| viewMappings.Count == 0)
{
viewMappings.Add(viewMapping);
view.EntityTypeMappings.Add(viewMapping);
}
}

viewMappings?.Reverse();
Expand Down Expand Up @@ -550,11 +598,43 @@ private static void AddSqlQueries(RelationalModel databaseModel, IEntityType ent
};

foreach (var property in mappedType.GetProperties())
{
var pseudoProperties = property.GetPseudoProperties();
if (pseudoProperties.Any())
{
foreach (var pseudoProperty in pseudoProperties)
{
MakeColumnMapping(pseudoProperty);
}
}
else
{
MakeColumnMapping(property);
}
}

mappedType = mappedType.BaseType;

queryMappings = entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.SqlQueryMappings) as List<SqlQueryMapping>;
if (queryMappings == null)
{
queryMappings = new List<SqlQueryMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.SqlQueryMappings, queryMappings);
}

if (queryMapping.ColumnMappings.Count != 0
|| queryMappings.Count == 0)
{
queryMappings.Add(queryMapping);
sqlQuery.EntityTypeMappings.Add(queryMapping);
}

void MakeColumnMapping(IProperty property)
{
var columnName = property.GetColumnName(mappedQuery);
if (columnName == null)
{
continue;
return;
}

var column = (SqlQueryColumn?)sqlQuery.FindColumn(columnName);
Expand Down Expand Up @@ -582,22 +662,6 @@ private static void AddSqlQueries(RelationalModel databaseModel, IEntityType ent

columnMappings.Add(columnMapping);
}

mappedType = mappedType.BaseType;

queryMappings = entityType.FindRuntimeAnnotationValue(RelationalAnnotationNames.SqlQueryMappings) as List<SqlQueryMapping>;
if (queryMappings == null)
{
queryMappings = new List<SqlQueryMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.SqlQueryMappings, queryMappings);
}

if (queryMapping.ColumnMappings.Count != 0
|| queryMappings.Count == 0)
{
queryMappings.Add(queryMapping);
sqlQuery.EntityTypeMappings.Add(queryMapping);
}
}

queryMappings?.Reverse();
Expand Down Expand Up @@ -699,11 +763,27 @@ private static FunctionMapping CreateFunctionMapping(
};

foreach (var property in mappedType.GetProperties())
{
var pseudoProperties = property.GetPseudoProperties();
if (pseudoProperties.Any())
{
foreach (var pseudoProperty in pseudoProperties)
{
MakeColumnMapping(pseudoProperty);
}
}
else
{
MakeColumnMapping(property);
}
}

void MakeColumnMapping(IProperty property)
{
var columnName = property.GetColumnName(mappedFunction);
if (columnName == null)
{
continue;
return;
}

var column = (FunctionColumn?)storeFunction.FindColumn(columnName);
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Migrations/HistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected virtual string MigrationIdColumnName
=> _migrationIdColumnName ??= EnsureModel()
.FindEntityType(typeof(HistoryRow))!
.FindProperty(nameof(HistoryRow.MigrationId))!
.GetColumnBaseName();
.GetColumnBaseName()!;

private IModel EnsureModel()
{
Expand Down Expand Up @@ -121,7 +121,7 @@ protected virtual string ProductVersionColumnName
=> _productVersionColumnName ??= EnsureModel()
.FindEntityType(typeof(HistoryRow))!
.FindProperty(nameof(HistoryRow.ProductVersion))!
.GetColumnBaseName();
.GetColumnBaseName()!;

/// <summary>
/// Overridden by database providers to generate SQL that tests for existence of the history table.
Expand Down
Loading