Skip to content

Commit

Permalink
Add ForeignKeyConstraint to the relational model.
Browse files Browse the repository at this point in the history
Part of #12846
  • Loading branch information
AndriySvyryd committed Mar 4, 2020
1 parent 725adf7 commit b453670
Show file tree
Hide file tree
Showing 57 changed files with 769 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,10 @@ protected virtual void GenerateForeignKeyAnnotations(

var annotations = foreignKey.GetAnnotations().ToList();

IgnoreAnnotations(
annotations,
RelationalAnnotationNames.ForeignKeyMappings);

GenerateFluentApiForAnnotation(
ref annotations,
RelationalAnnotationNames.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ private IEnumerable<string> GetAnnotationNamespaces(IEnumerable<IAnnotatable> it
RelationalAnnotationNames.TableColumnMappings,
RelationalAnnotationNames.Views,
RelationalAnnotationNames.ViewMappings,
RelationalAnnotationNames.ViewColumnMappings
RelationalAnnotationNames.ViewColumnMappings,
RelationalAnnotationNames.ForeignKeyMappings
};

return items.SelectMany(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotation
var canUseDataAnnotations = true;
var annotations = foreignKey.GetAnnotations().ToList();

RemoveAnnotation(ref annotations, RelationalAnnotationNames.ForeignKeyMappings);

var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.HasOne)}("
Expand Down
19 changes: 12 additions & 7 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -164,25 +165,29 @@ public static void SetSchema(
/// Returns the tables to which the entity type is mapped.
/// </summary>
/// <param name="entityType"> The entity type to get the table name for. </param>
/// <returns> The name of the table to which the entity type is mapped. </returns>
/// <returns> The tables to which the entity type is mapped. </returns>
public static IEnumerable<ITableMapping> GetTableMappings([NotNull] this IEntityType entityType) =>
(IEnumerable<ITableMapping>)entityType[RelationalAnnotationNames.TableMappings];
(IEnumerable<ITableMapping>)entityType[RelationalAnnotationNames.TableMappings]
?? Enumerable.Empty<ITableMapping>();

/// <summary>
/// Returns the views or tables to which the entity type is mapped.
/// Returns the views to which the entity type is mapped or the tables of it's not mapped to any views.
/// </summary>
/// <param name="entityType"> The entity type to get the table name for. </param>
/// <returns> The name of the table to which the entity type is mapped. </returns>
/// <returns> The views or tables to which the entity type is mapped. </returns>
public static IEnumerable<ITableMappingBase> GetViewOrTableMappings([NotNull] this IEntityType entityType) =>
(IEnumerable<ITableMappingBase>)GetViewMappings(entityType) ?? GetTableMappings(entityType);
(IEnumerable<ITableMappingBase>)(entityType[RelationalAnnotationNames.ViewMappings]
?? entityType[RelationalAnnotationNames.TableMappings])
?? Enumerable.Empty<ITableMappingBase>();

/// <summary>
/// Returns the views to which the entity type is mapped.
/// </summary>
/// <param name="entityType"> The entity type to get the table name for. </param>
/// <returns> The name of the table to which the entity type is mapped. </returns>
/// <returns> The views to which the entity type is mapped. </returns>
public static IEnumerable<IViewMapping> GetViewMappings([NotNull] this IEntityType entityType) =>
(IEnumerable<IViewMapping>)entityType[RelationalAnnotationNames.ViewMappings];
(IEnumerable<IViewMapping>)entityType[RelationalAnnotationNames.ViewMappings]
?? Enumerable.Empty<IViewMapping>();

/// <summary>
/// Returns the name of the view to which the entity type is mapped.
Expand Down
10 changes: 10 additions & 0 deletions src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
Expand Down Expand Up @@ -75,5 +76,14 @@ public static void SetConstraintName(
public static ConfigurationSource? GetConstraintNameConfigurationSource([NotNull] this IConventionForeignKey foreignKey)
=> foreignKey.FindAnnotation(RelationalAnnotationNames.Name)
?.GetConfigurationSource();

/// <summary>
/// Gets the foreign key constraints to which the foreign key is mapped.
/// </summary>
/// <param name="foreignKey"> The foreign key. </param>
/// <returns> The foreign key constraints to which the foreign key is mapped. </returns>
public static IEnumerable<IForeignKeyConstraint> GetConstraintMappings([NotNull] this IForeignKey foreignKey) =>
(IEnumerable<IForeignKeyConstraint>)foreignKey[RelationalAnnotationNames.ForeignKeyMappings]
?? Enumerable.Empty<IForeignKeyConstraint>();
}
}
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Extensions/RelationalModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public static ITable FindTable([NotNull] this IModel model, [NotNull] string nam
/// <summary>
/// Returns all the views mapped in the model.
/// </summary>
/// <param name="model"> The model to get the tables for. </param>
/// <returns> All the tables mapped in the model. </returns>
/// <param name="model"> The model to get the views for. </param>
/// <returns> All the views mapped in the model. </returns>
public static IEnumerable<IView> GetViews([NotNull] this IModel model) =>
((IDictionary<(string, string), View>)model[RelationalAnnotationNames.Views])?.Values
?? Enumerable.Empty<IView>();
Expand Down
10 changes: 7 additions & 3 deletions src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,27 @@ public static void SetColumnType(
/// <param name="property"> The property. </param>
/// <returns> The table columns to which the property is mapped. </returns>
public static IEnumerable<IColumnMapping> GetTableColumnMappings([NotNull] this IProperty property) =>
(IEnumerable<IColumnMapping>)property[RelationalAnnotationNames.TableColumnMappings];
(IEnumerable<IColumnMapping>)property[RelationalAnnotationNames.TableColumnMappings]
?? Enumerable.Empty<IColumnMapping>();

/// <summary>
/// Returns the view or table columns to which the property is mapped.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The view or table columns to which the property is mapped. </returns>
public static IEnumerable<IColumnMappingBase> GetViewOrTableColumnMappings([NotNull] this IProperty property) =>
(IEnumerable<IColumnMappingBase>)GetViewColumnMappings(property) ?? GetTableColumnMappings(property);
(IEnumerable<IColumnMappingBase>)(property[RelationalAnnotationNames.ViewColumnMappings]
?? property[RelationalAnnotationNames.TableColumnMappings])
?? Enumerable.Empty<IColumnMappingBase>();

/// <summary>
/// Returns the view columns to which the property is mapped.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The view columns to which the property is mapped. </returns>
public static IEnumerable<IViewColumnMapping> GetViewColumnMappings([NotNull] this IProperty property) =>
(IEnumerable<IViewColumnMapping>)property[RelationalAnnotationNames.ViewColumnMappings];
(IEnumerable<IViewColumnMapping>)property[RelationalAnnotationNames.ViewColumnMappings]
?? Enumerable.Empty<IViewColumnMapping>();

/// <summary>
/// Returns the SQL expression that is used as the default value for the column this property is mapped to.
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/ICheckConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ICheckConstraint
string Name { get; }

/// <summary>
/// The <see cref="IEntityType" /> in which this check constraint is defined.
/// Gets the <see cref="IEntityType" /> in which this check constraint is defined.
/// </summary>
IEntityType EntityType { get; }

Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Metadata/IColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IColumn : IColumnBase
{
/// <summary>
/// The containing table.
/// Gets the containing table.
/// </summary>
new ITable Table { get; }

/// <summary>
/// The property mappings.
/// Gets the property mappings.
/// </summary>
new IEnumerable<IColumnMapping> PropertyMappings { get; }
}
Expand Down
10 changes: 5 additions & 5 deletions src/EFCore.Relational/Metadata/IColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IColumnBase : IAnnotatable
{
/// <summary>
/// The column name.
/// Gets the column name.
/// </summary>
string Name { get; }

/// <summary>
/// The column type.
/// Gets the column type.
/// </summary>
string Type { get; }

/// <summary>
/// Whether the column can contain NULL.
/// Gets the value indicating whether the column can contain NULL.
/// </summary>
bool IsNullable { get; }

/// <summary>
/// The containing table-like object.
/// Gets the containing table-like object.
/// </summary>
ITableBase Table { get; }

/// <summary>
/// The property mappings.
/// Gets the property mappings.
/// </summary>
IEnumerable<IColumnMappingBase> PropertyMappings { get; }
}
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Metadata/IColumnMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IColumnMapping : IColumnMappingBase
{
/// <summary>
/// The target column.
/// Gets the target column.
/// </summary>
new IColumn Column { get; }

/// <summary>
/// The containing table mapping.
/// Gets the containing table mapping.
/// </summary>
new ITableMapping TableMapping { get; }
}
Expand Down
8 changes: 4 additions & 4 deletions src/EFCore.Relational/Metadata/IColumnMappingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IColumnMappingBase : IAnnotatable
{
/// <summary>
/// The mapped property.
/// Gets the mapped property.
/// </summary>
IProperty Property { get; }

/// <summary>
/// The target column-like object.
/// Gets the target column-like object.
/// </summary>
IColumnBase Column { get; }

/// <summary>
/// The type mapping for the column-like object.
/// Gets the type mapping for the column-like object.
/// </summary>
RelationalTypeMapping TypeMapping { get; }

/// <summary>
/// The containing table mapping.
/// Gets the containing table mapping.
/// </summary>
ITableMappingBase TableMapping { get; }
}
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Metadata/IConventionCheckConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IConventionCheckConstraint : ICheckConstraint
{
/// <summary>
/// The <see cref="IConventionEntityType" /> in which this check constraint is defined.
/// Gets the <see cref="IConventionEntityType" /> in which this check constraint is defined.
/// </summary>
new IConventionEntityType EntityType { get; }

/// <summary>
/// Returns the configuration source for this <see cref="IConventionCheckConstraint" />.
/// Gets the configuration source for this <see cref="IConventionCheckConstraint" />.
/// </summary>
/// <returns> The configuration source for <see cref="IConventionCheckConstraint" />. </returns>
ConfigurationSource GetConfigurationSource();
Expand Down
18 changes: 9 additions & 9 deletions src/EFCore.Relational/Metadata/IConventionDbFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IConventionDbFunction : IDbFunction
{
/// <summary>
/// The <see cref="IConventionModel" /> in which this function is defined.
/// Gets the <see cref="IConventionModel" /> in which this function is defined.
/// </summary>
new IConventionModel Model { get; }

/// <summary>
/// The builder that can be used to configure this function.
/// Gets the builder that can be used to configure this function.
/// </summary>
IConventionDbFunctionBuilder Builder { get; }

/// <summary>
/// Returns the configuration source for this <see cref="IConventionDbFunction" />.
/// Gets the configuration source for this <see cref="IConventionDbFunction" />.
/// </summary>
/// <returns> The configuration source for <see cref="IConventionDbFunction" />. </returns>
ConfigurationSource GetConfigurationSource();
Expand All @@ -40,7 +40,7 @@ public interface IConventionDbFunction : IDbFunction
void SetName([CanBeNull] string name, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunction.Name" />.
/// Gets the configuration source for <see cref="IDbFunction.Name" />.
/// </summary>
/// <returns> The configuration source for <see cref="IDbFunction.Name" />. </returns>
ConfigurationSource? GetNameConfigurationSource();
Expand All @@ -53,7 +53,7 @@ public interface IConventionDbFunction : IDbFunction
void SetSchema([CanBeNull] string schema, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunction.Schema" />.
/// Gets the configuration source for <see cref="IDbFunction.Schema" />.
/// </summary>
/// <returns> The configuration source for <see cref="IDbFunction.Schema" />. </returns>
ConfigurationSource? GetSchemaConfigurationSource();
Expand All @@ -66,7 +66,7 @@ public interface IConventionDbFunction : IDbFunction
void SetStoreType([CanBeNull] string storeType, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunction.StoreType" />.
/// Gets the configuration source for <see cref="IDbFunction.StoreType" />.
/// </summary>
/// <returns> The configuration source for <see cref="IDbFunction.StoreType" />. </returns>
ConfigurationSource? GetStoreTypeConfigurationSource();
Expand All @@ -79,7 +79,7 @@ public interface IConventionDbFunction : IDbFunction
void SetTypeMapping([CanBeNull] RelationalTypeMapping typeMapping, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunction.TypeMapping" />.
/// Gets the configuration source for <see cref="IDbFunction.TypeMapping" />.
/// </summary>
/// <returns> The configuration source for <see cref="IDbFunction.TypeMapping" />. </returns>
ConfigurationSource? GetTypeMappingConfigurationSource();
Expand All @@ -95,13 +95,13 @@ void SetTranslation(
[CanBeNull] Func<IReadOnlyCollection<SqlExpression>, SqlExpression> translation, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunction.Translation" />.
/// Gets the configuration source for <see cref="IDbFunction.Translation" />.
/// </summary>
/// <returns> The configuration source for <see cref="IDbFunction.Translation" />. </returns>
ConfigurationSource? GetTranslationConfigurationSource();

/// <summary>
/// The parameters for this function
/// Gets the parameters for this function
/// </summary>
new IReadOnlyList<IConventionDbFunctionParameter> Parameters { get; }
}
Expand Down
12 changes: 6 additions & 6 deletions src/EFCore.Relational/Metadata/IConventionSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata
public interface IConventionSequence : ISequence
{
/// <summary>
/// The <see cref="IConventionModel" /> in which this sequence is defined.
/// Gets the <see cref="IConventionModel" /> in which this sequence is defined.
/// </summary>
new IConventionModel Model { get; }

Expand All @@ -24,7 +24,7 @@ public interface IConventionSequence : ISequence
IConventionSequenceBuilder Builder { get; }

/// <summary>
/// Returns the configuration source for this <see cref="IConventionSequence" />.
/// Gets the configuration source for this <see cref="IConventionSequence" />.
/// </summary>
/// <returns> The configuration source for <see cref="IConventionSequence" />. </returns>
ConfigurationSource GetConfigurationSource();
Expand All @@ -50,7 +50,7 @@ public interface IConventionSequence : ISequence
void SetIncrementBy(int? incrementBy, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="ISequence.IncrementBy" />.
/// Gets the configuration source for <see cref="ISequence.IncrementBy" />.
/// </summary>
/// <returns> The configuration source for <see cref="ISequence.IncrementBy" />. </returns>
ConfigurationSource? GetIncrementByConfigurationSource();
Expand All @@ -76,7 +76,7 @@ public interface IConventionSequence : ISequence
void SetMaxValue(long? maxValue, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="ISequence.MaxValue" />.
/// Gets the configuration source for <see cref="ISequence.MaxValue" />.
/// </summary>
/// <returns> The configuration source for <see cref="ISequence.MaxValue" />. </returns>
ConfigurationSource? GetMaxValueConfigurationSource();
Expand All @@ -89,7 +89,7 @@ public interface IConventionSequence : ISequence
void SetClrType([CanBeNull] Type clrType, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="ISequence.ClrType" />.
/// Gets the configuration source for <see cref="ISequence.ClrType" />.
/// </summary>
/// <returns> The configuration source for <see cref="ISequence.ClrType" />. </returns>
ConfigurationSource? GetClrTypeConfigurationSource();
Expand All @@ -105,7 +105,7 @@ public interface IConventionSequence : ISequence
void SetIsCyclic(bool? cyclic, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="ISequence.IsCyclic" />.
/// Gets the configuration source for <see cref="ISequence.IsCyclic" />.
/// </summary>
/// <returns> The configuration source for <see cref="ISequence.IsCyclic" />. </returns>
ConfigurationSource? GetIsCyclicConfigurationSource();
Expand Down
Loading

0 comments on commit b453670

Please sign in to comment.