|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using JetBrains.Annotations; |
| 5 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 6 | +using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| 7 | +using Microsoft.EntityFrameworkCore.Metadata.Internal; |
| 8 | +using Microsoft.EntityFrameworkCore.Utilities; |
| 9 | + |
| 10 | +// ReSharper disable once CheckNamespace |
| 11 | +namespace Microsoft.EntityFrameworkCore |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Relational database specific extension methods for <see cref="CollectionOwnershipBuilder" />. |
| 15 | + /// </summary> |
| 16 | + public static class RelationalCollectionOwnershipBuilderExtensions |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Configures the view or table that the entity maps to when targeting a relational database. |
| 20 | + /// </summary> |
| 21 | + /// <param name="collectionOwnershipBuilder"> The builder for the entity type being configured. </param> |
| 22 | + /// <param name="name"> The name of the view or table. </param> |
| 23 | + /// <returns> The same builder instance so that multiple calls can be chained. </returns> |
| 24 | + public static CollectionOwnershipBuilder ToTable( |
| 25 | + [NotNull] this CollectionOwnershipBuilder collectionOwnershipBuilder, |
| 26 | + [CanBeNull] string name) |
| 27 | + { |
| 28 | + Check.NotNull(collectionOwnershipBuilder, nameof(collectionOwnershipBuilder)); |
| 29 | + Check.NullButNotEmpty(name, nameof(name)); |
| 30 | + |
| 31 | + collectionOwnershipBuilder.GetInfrastructure<InternalEntityTypeBuilder>() |
| 32 | + .Relational(ConfigurationSource.Explicit) |
| 33 | + .ToTable(name); |
| 34 | + |
| 35 | + return collectionOwnershipBuilder; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Configures the view or table that the entity maps to when targeting a relational database. |
| 40 | + /// </summary> |
| 41 | + /// <typeparam name="TEntity"> The entity type being configured. </typeparam> |
| 42 | + /// <typeparam name="TDependentEntity"> The entity type that this relationship targets. </typeparam> |
| 43 | + /// <param name="collectionOwnershipBuilder"> The builder for the entity type being configured. </param> |
| 44 | + /// <param name="name"> The name of the view or table. </param> |
| 45 | + /// <returns> The same builder instance so that multiple calls can be chained. </returns> |
| 46 | + public static CollectionOwnershipBuilder<TEntity, TDependentEntity> ToTable<TEntity, TDependentEntity>( |
| 47 | + [NotNull] this CollectionOwnershipBuilder<TEntity, TDependentEntity> collectionOwnershipBuilder, |
| 48 | + [CanBeNull] string name) |
| 49 | + where TEntity : class |
| 50 | + where TDependentEntity : class |
| 51 | + => (CollectionOwnershipBuilder<TEntity, TDependentEntity>)ToTable((CollectionOwnershipBuilder)collectionOwnershipBuilder, name); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Configures the view or table that the entity maps to when targeting a relational database. |
| 55 | + /// </summary> |
| 56 | + /// <param name="collectionOwnershipBuilder"> The builder for the entity type being configured. </param> |
| 57 | + /// <param name="name"> The name of the view or table. </param> |
| 58 | + /// <param name="schema"> The schema of the view or table. </param> |
| 59 | + /// <returns> The same builder instance so that multiple calls can be chained. </returns> |
| 60 | + public static CollectionOwnershipBuilder ToTable( |
| 61 | + [NotNull] this CollectionOwnershipBuilder collectionOwnershipBuilder, |
| 62 | + [CanBeNull] string name, |
| 63 | + [CanBeNull] string schema) |
| 64 | + { |
| 65 | + Check.NotNull(collectionOwnershipBuilder, nameof(collectionOwnershipBuilder)); |
| 66 | + Check.NullButNotEmpty(name, nameof(name)); |
| 67 | + Check.NullButNotEmpty(schema, nameof(schema)); |
| 68 | + |
| 69 | + collectionOwnershipBuilder.GetInfrastructure<InternalEntityTypeBuilder>() |
| 70 | + .Relational(ConfigurationSource.Explicit) |
| 71 | + .ToTable(name, schema); |
| 72 | + |
| 73 | + return collectionOwnershipBuilder; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Configures the view or table that the entity maps to when targeting a relational database. |
| 78 | + /// </summary> |
| 79 | + /// <typeparam name="TEntity"> The entity type being configured. </typeparam> |
| 80 | + /// <typeparam name="TDependentEntity"> The entity type that this relationship targets. </typeparam> |
| 81 | + /// <param name="collectionOwnershipBuilder"> The builder for the entity type being configured. </param> |
| 82 | + /// <param name="name"> The name of the view or table. </param> |
| 83 | + /// <param name="schema"> The schema of the view or table. </param> |
| 84 | + /// <returns> The same builder instance so that multiple calls can be chained. </returns> |
| 85 | + public static CollectionOwnershipBuilder<TEntity, TDependentEntity> ToTable<TEntity, TDependentEntity>( |
| 86 | + [NotNull] this CollectionOwnershipBuilder<TEntity, TDependentEntity> collectionOwnershipBuilder, |
| 87 | + [CanBeNull] string name, |
| 88 | + [CanBeNull] string schema) |
| 89 | + where TEntity : class |
| 90 | + where TDependentEntity : class |
| 91 | + => (CollectionOwnershipBuilder<TEntity, TDependentEntity>)ToTable((CollectionOwnershipBuilder)collectionOwnershipBuilder, name, schema); |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Configures the foreign key constraint name for this relationship when targeting a relational database. |
| 95 | + /// </summary> |
| 96 | + /// <param name="referenceReferenceBuilder"> The builder being used to configure the relationship. </param> |
| 97 | + /// <param name="name"> The name of the foreign key constraint. </param> |
| 98 | + /// <returns> The same builder instance so that multiple calls can be chained. </returns> |
| 99 | + public static CollectionOwnershipBuilder HasConstraintName( |
| 100 | + [NotNull] this CollectionOwnershipBuilder referenceReferenceBuilder, |
| 101 | + [CanBeNull] string name) |
| 102 | + { |
| 103 | + Check.NotNull(referenceReferenceBuilder, nameof(referenceReferenceBuilder)); |
| 104 | + Check.NullButNotEmpty(name, nameof(name)); |
| 105 | + |
| 106 | + referenceReferenceBuilder.GetInfrastructure<InternalRelationshipBuilder>() |
| 107 | + .Relational(ConfigurationSource.Explicit) |
| 108 | + .HasConstraintName(name); |
| 109 | + |
| 110 | + return referenceReferenceBuilder; |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Configures the foreign key constraint name for this relationship when targeting a relational database. |
| 115 | + /// </summary> |
| 116 | + /// <param name="referenceReferenceBuilder"> The builder being used to configure the relationship. </param> |
| 117 | + /// <param name="name"> The name of the foreign key constraint. </param> |
| 118 | + /// <returns> The same builder instance so that multiple calls can be chained. </returns> |
| 119 | + /// <typeparam name="TEntity"> The entity type on one end of the relationship. </typeparam> |
| 120 | + /// <typeparam name="TDependentEntity"> The entity type on the other end of the relationship. </typeparam> |
| 121 | + public static CollectionOwnershipBuilder<TEntity, TDependentEntity> HasConstraintName<TEntity, TDependentEntity>( |
| 122 | + [NotNull] this CollectionOwnershipBuilder<TEntity, TDependentEntity> referenceReferenceBuilder, |
| 123 | + [CanBeNull] string name) |
| 124 | + where TEntity : class |
| 125 | + where TDependentEntity : class |
| 126 | + => (CollectionOwnershipBuilder<TEntity, TDependentEntity>)HasConstraintName( |
| 127 | + (CollectionOwnershipBuilder)referenceReferenceBuilder, name); |
| 128 | + } |
| 129 | +} |
0 commit comments