Skip to content

Commit

Permalink
Move ModelBuilding tests to EFCore.Specification.Tests
Browse files Browse the repository at this point in the history
Fix found issues

Part of #32315
  • Loading branch information
AndriySvyryd committed Dec 6, 2023
1 parent 32c6133 commit 5547a04
Show file tree
Hide file tree
Showing 80 changed files with 4,387 additions and 2,603 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public SnapshotModelProcessor(
_relationalNames = new HashSet<string>(
typeof(RelationalAnnotationNames)
.GetRuntimeFields()
.Where(p => p.Name != nameof(RelationalAnnotationNames.Prefix))
.Where(p => p.Name != nameof(RelationalAnnotationNames.Prefix)
&& p.Name != nameof(RelationalAnnotationNames.AllNames))
.Select(p => (string)p.GetValue(null)!)
.Where(v => v.IndexOf(':') > 0)
.Select(v => v[(RelationalAnnotationNames.Prefix.Length - 1)..]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,18 @@ void CreateMappings(
}

// All the mappings below are added in a way that preserves the order
foreach (var mapping in typeBase.GetDefaultMappings())
if (typeBase.GetDefaultMappings().Any())
{
var tableMappingsVariable = code.Identifier("defaultTableMappings", parameters.ScopeVariables, capitalize: false);
mainBuilder
.AppendLine()
.AppendLine($"var {tableMappingsVariable} = new List<TableMappingBase<ColumnMappingBase>>();")
.Append($"{typeBaseVariable}.SetRuntimeAnnotation(")
.AppendLine($"{code.Literal(RelationalAnnotationNames.DefaultMappings)}, {tableMappingsVariable});");
Create(mapping, tableMappingsVariable, metadataVariables, parameters);
foreach (var mapping in typeBase.GetDefaultMappings())
{
Create(mapping, tableMappingsVariable, metadataVariables, parameters);
}
}

if (typeBase.GetTableMappings().Any())
Expand Down Expand Up @@ -2090,8 +2093,10 @@ public override void Generate(IForeignKey foreignKey, CSharpRuntimeAnnotationCod
/// <inheritdoc />
public override void Generate(IIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
var annotations = parameters.Annotations;
annotations.Remove(parameters.IsRuntime ? RelationalAnnotationNames.TableIndexMappings : RelationalAnnotationNames.Filter);
if (parameters.IsRuntime)
{
parameters.Annotations.Remove(RelationalAnnotationNames.TableIndexMappings);
}

base.Generate(index, parameters);
}
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.Relational/Metadata/Internal/ColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ public virtual bool AddPropertyMapping(TColumnMappingBase columnMapping)
return true;
}

/// <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 void RemovePropertyMapping(TColumnMappingBase columnMapping)
=> PropertyMappings.RemoveAt(PropertyMappings.IndexOf(columnMapping, ColumnMappingBaseComparer.Instance));

/// <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
10 changes: 4 additions & 6 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.Data;
using System.Text.Json;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand Down Expand Up @@ -1435,11 +1436,11 @@ private static void PopulateRowInternalForeignKeys<TColumnMapping>(TableBase tab
{
TableMappingBase<TColumnMapping>? mainMapping = null;
var mappedEntityTypes = new HashSet<IEntityType>();
foreach (TableMappingBase<TColumnMapping> entityTypeMapping in table.EntityTypeMappings)
foreach (TableMappingBase<TColumnMapping> entityTypeMapping in table.EntityTypeMappings.ToList())
{
if (table.EntityTypeMappings.Count > 1)
{
entityTypeMapping.IsSharedTablePrincipal = false;
entityTypeMapping.SetIsSharedTablePrincipal(false);
}

var entityType = (IEntityType)entityTypeMapping.TypeBase;
Expand Down Expand Up @@ -1489,10 +1490,7 @@ private static void PopulateRowInternalForeignKeys<TColumnMapping>(TableBase tab

if (table.EntityTypeMappings.Count > 1)
{
// Re-add the mapping to update the order
mainMapping.Table.EntityTypeMappings.Remove(mainMapping);
mainMapping.IsSharedTablePrincipal = true;
mainMapping.Table.EntityTypeMappings.Add(mainMapping);
mainMapping.SetIsSharedTablePrincipal(true);
}

var referencingInternalForeignKeyMap = table.ReferencingRowInternalForeignKeys;
Expand Down
40 changes: 40 additions & 0 deletions src/EFCore.Relational/Metadata/Internal/TableMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,46 @@ public TableMapping(
/// </summary>
public virtual IStoredProcedureMapping? UpdateStoredProcedureMapping { get; set; }

/// <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 void SetIsSharedTablePrincipal(bool isSharedTablePrincipal)
{
if (IsSharedTablePrincipal == isSharedTablePrincipal)
{
return;
}

((Table)Table).EntityTypeMappings.Remove(this);

var removedColumnMappings = new List<ColumnMapping>();
foreach (ColumnMapping columnMapping in ((ITableMapping)this).ColumnMappings)
{
((Column)columnMapping.Column).RemovePropertyMapping(columnMapping);
var columnMappings = (SortedSet<ColumnMapping>)columnMapping.Property.FindRuntimeAnnotationValue(
RelationalAnnotationNames.TableColumnMappings)!;
columnMappings.Remove(columnMapping);

removedColumnMappings.Add(columnMapping);
}

IsSharedTablePrincipal = isSharedTablePrincipal;

// Re-add the mappings to update the order
((Table)Table).EntityTypeMappings.Add(this);

foreach (var columnMapping in removedColumnMappings)
{
((Column)columnMapping.Column).AddPropertyMapping(columnMapping);
var columnMappings = (SortedSet<ColumnMapping>)columnMapping.Property.FindRuntimeAnnotationValue(
RelationalAnnotationNames.TableColumnMappings)!;
columnMappings.Add(columnMapping);
}
}

/// <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
11 changes: 10 additions & 1 deletion src/EFCore.Relational/Metadata/Internal/TableMappingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ public virtual bool AddColumnMapping(TColumnMapping columnMapping)
public virtual bool? IsSharedTablePrincipal { get; set; }

/// <inheritdoc />
public virtual bool? IsSplitEntityTypePrincipal { get; set; }
public virtual bool? IsSplitEntityTypePrincipal { get; init; }

/// <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 void SetIsSharedTablePrincipal(bool isSharedTablePrincipal)
=> throw new NotImplementedException();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
40 changes: 40 additions & 0 deletions src/EFCore.Relational/Metadata/Internal/ViewMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ public ViewMapping(
public virtual IView View
=> (IView)base.Table;

/// <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 void SetIsSharedTablePrincipal(bool isSharedTablePrincipal)
{
if (IsSharedTablePrincipal == isSharedTablePrincipal)
{
return;
}

((View)View).EntityTypeMappings.Remove(this);

var removedColumnMappings = new List<ViewColumnMapping>();
foreach (ViewColumnMapping columnMapping in ((IViewMapping)this).ColumnMappings)
{
((ViewColumn)columnMapping.Column).RemovePropertyMapping(columnMapping);
var columnMappings = (SortedSet<ViewColumnMapping>)columnMapping.Property.FindRuntimeAnnotationValue(
RelationalAnnotationNames.ViewColumnMappings)!;
columnMappings.Remove(columnMapping);

removedColumnMappings.Add(columnMapping);
}

IsSharedTablePrincipal = isSharedTablePrincipal;

// Re-add the mappings to update the order
((View)View).EntityTypeMappings.Add(this);

foreach (var columnMapping in removedColumnMappings)
{
((ViewColumn)columnMapping.Column).AddPropertyMapping(columnMapping);
var columnMappings = (SortedSet<ViewColumnMapping>)columnMapping.Property.FindRuntimeAnnotationValue(
RelationalAnnotationNames.ViewColumnMappings)!;
columnMappings.Add(columnMapping);
}
}

/// <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
74 changes: 74 additions & 0 deletions src/EFCore.Relational/Metadata/RelationalAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,78 @@ public static class RelationalAnnotationNames
/// The name for store (database) type annotations.
/// </summary>
public const string StoreType = Prefix + "StoreType";

/// <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 static readonly ISet<string> AllNames = new HashSet<string>
{
ColumnName,
ColumnOrder,
ColumnType,
DefaultValueSql,
ComputedColumnSql,
IsStored,
DefaultValue,
TableName,
Schema,
ViewName,
ViewSchema,
FunctionName,
DeleteStoredProcedure,
InsertStoredProcedure,
UpdateStoredProcedure,
SqlQuery,
Comment,
Collation,
DefaultSchema,
Name,
#pragma warning disable CS0618 // Type or member is obsolete
SequencePrefix,
#pragma warning restore CS0618 // Type or member is obsolete
Sequences,
CheckConstraints,
Filter,
DbFunctions,
MaxIdentifierLength,
IsFixedLength,
ViewDefinitionSql,
IsTableExcludedFromMigrations,
MappingStrategy,
RelationalModel,
DefaultMappings,
DefaultColumnMappings,
TableMappings,
TableColumnMappings,
ViewMappings,
ViewColumnMappings,
FunctionMappings,
FunctionColumnMappings,
InsertStoredProcedureMappings,
InsertStoredProcedureResultColumnMappings,
InsertStoredProcedureParameterMappings,
DeleteStoredProcedureMappings,
DeleteStoredProcedureParameterMappings,
UpdateStoredProcedureMappings,
UpdateStoredProcedureResultColumnMappings,
UpdateStoredProcedureParameterMappings,
SqlQueryMappings,
SqlQueryColumnMappings,
ForeignKeyMappings,
TableIndexMappings,
UniqueConstraintMappings,
MappingFragments,
RelationalOverrides,
ModelDependencies,
FieldValueGetter,
ContainerColumnName,
#pragma warning disable CS0618 // Type or member is obsolete
ContainerColumnTypeMapping,
#pragma warning restore CS0618 // Type or member is obsolete
JsonPropertyName,
StoreType
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<None Update="cosmosConfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Northwind.json">
Expand Down
Loading

0 comments on commit 5547a04

Please sign in to comment.