Exemple #1
0
    /// <summary>
    ///     Validates relationships.
    /// </summary>
    /// <param name="model">The model.</param>
    /// <param name="logger">The logger to use.</param>
    protected virtual void ValidateRelationships(
        IModel model,
        IDiagnosticsLogger <DbLoggerCategory.Model.Validation> logger)
    {
        foreach (var entityType in model.GetEntityTypes())
        {
            foreach (var foreignKey in entityType.GetDeclaredForeignKeys())
            {
                if (foreignKey.IsUnique &&
                    foreignKey is IConventionForeignKey concreteFk &&
                    concreteFk.GetPrincipalEndConfigurationSource() == null)
                {
                    throw new InvalidOperationException(
                              CoreStrings.AmbiguousOneToOneRelationship(
                                  foreignKey.DeclaringEntityType.DisplayName()
                                  + (foreignKey.DependentToPrincipal == null
                                ? ""
                                : "." + foreignKey.DependentToPrincipal.Name),
                                  foreignKey.PrincipalEntityType.DisplayName()
                                  + (foreignKey.PrincipalToDependent == null
                                ? ""
                                : "." + foreignKey.PrincipalToDependent.Name)));
                }
            }

            foreach (var skipNavigation in entityType.GetDeclaredSkipNavigations())
            {
                if (!skipNavigation.IsCollection)
                {
                    throw new InvalidOperationException(
                              CoreStrings.SkipNavigationNonCollection(
                                  skipNavigation.Name, skipNavigation.DeclaringEntityType.DisplayName()));
                }

                if (skipNavigation.ForeignKey == null)
                {
                    throw new InvalidOperationException(
                              CoreStrings.SkipNavigationNoForeignKey(
                                  skipNavigation.Name, skipNavigation.DeclaringEntityType.DisplayName()));
                }

                if (skipNavigation.Inverse == null)
                {
                    throw new InvalidOperationException(
                              CoreStrings.SkipNavigationNoInverse(
                                  skipNavigation.Name, skipNavigation.DeclaringEntityType.DisplayName()));
                }

                if (skipNavigation.IsShadowProperty())
                {
                    throw new InvalidOperationException(
                              CoreStrings.ShadowManyToManyNavigation(
                                  skipNavigation.DeclaringEntityType.DisplayName(),
                                  skipNavigation.Name,
                                  skipNavigation.Inverse.DeclaringEntityType.DisplayName(),
                                  skipNavigation.Inverse.Name));
                }
            }
        }
    }
        /// <summary>
        ///     Validates relationships.
        /// </summary>
        /// <param name="model"> The model. </param>
        /// <param name="logger"> The logger to use. </param>
        protected virtual void ValidateRelationships(
            [NotNull] IModel model, [NotNull] IDiagnosticsLogger <DbLoggerCategory.Model.Validation> logger)
        {
            Check.NotNull(model, nameof(model));

            foreach (var entityType in model.GetEntityTypes())
            {
                foreach (var foreignKey in entityType.GetDeclaredForeignKeys())
                {
                    if (foreignKey.IsUnique &&
                        foreignKey is IConventionForeignKey concreteFk &&
                        concreteFk.GetPrincipalEndConfigurationSource() == null)
                    {
                        throw new InvalidOperationException(
                                  CoreStrings.AmbiguousOneToOneRelationship(
                                      foreignKey.DeclaringEntityType.DisplayName()
                                      + (foreignKey.DependentToPrincipal == null
                                    ? ""
                                    : "." + foreignKey.DependentToPrincipal.Name),
                                      foreignKey.PrincipalEntityType.DisplayName()
                                      + (foreignKey.PrincipalToDependent == null
                                    ? ""
                                    : "." + foreignKey.PrincipalToDependent.Name)));
                    }
                }
            }
        }
        /// <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 InternalModelBuilder Apply(InternalModelBuilder modelBuilder)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));

            foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
            {
                foreach (var foreignKey in entityType.GetDeclaredForeignKeys())
                {
                    if (foreignKey.IsUnique &&
                        foreignKey.GetPrincipalEndConfigurationSource() == null)
                    {
                        throw new InvalidOperationException(
                                  CoreStrings.AmbiguousOneToOneRelationship(
                                      foreignKey.DeclaringEntityType.DisplayName() + (foreignKey.DependentToPrincipal == null
                                    ? ""
                                    : "." + foreignKey.DependentToPrincipal.Name),
                                      foreignKey.PrincipalEntityType.DisplayName() + (foreignKey.PrincipalToDependent == null
                                    ? ""
                                    : "." + foreignKey.PrincipalToDependent.Name)));
                    }
                }
            }

            return(modelBuilder);
        }