Exemple #1
0
        /// <summary>
        ///     Returns the default key constraint name that would be used for this key.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <returns> The default key constraint name that would be used for this key. </returns>
        public static string GetDefaultName([NotNull] this IKey key)
        {
            var sharedTablePrincipalPrimaryKeyProperty = key.Properties[0].FindSharedRootPrimaryKeyProperty();

            if (sharedTablePrincipalPrimaryKeyProperty != null)
            {
                return(sharedTablePrincipalPrimaryKeyProperty.FindContainingPrimaryKey().GetName());
            }

            var builder   = new StringBuilder();
            var tableName = key.DeclaringEntityType.GetTableName() ?? key.DeclaringEntityType.GetViewName();

            if (key.IsPrimaryKey())
            {
                builder
                .Append("PK_")
                .Append(tableName);
            }
            else
            {
                builder
                .Append("AK_")
                .Append(tableName)
                .Append("_")
                .AppendJoin(key.Properties.Select(p => p.GetColumnName()), "_");
            }

            return(Uniquifier.Truncate(builder.ToString(), key.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
    /// <summary>
    ///     Returns the default table name that would be used for this entity type.
    /// </summary>
    /// <param name="entityType">The entity type to get the table name for.</param>
    /// <param name="truncate">A value indicating whether the name should be truncated to the max identifier length.</param>
    /// <returns>The default name of the table to which the entity type would be mapped.</returns>
    public static string?GetDefaultTableName(this IReadOnlyEntityType entityType, bool truncate = true)
    {
        var ownership = entityType.FindOwnership();

        if (ownership != null &&
            ownership.IsUnique)
        {
            return(ownership.PrincipalEntityType.GetTableName());
        }

        var name = entityType.ShortName();

        if (entityType.HasSharedClrType &&
            ownership != null
#pragma warning disable EF1001 // Internal EF Core API usage.
            && entityType.Name == ownership.PrincipalEntityType.GetOwnedName(name, ownership.PrincipalToDependent !.Name))
#pragma warning restore EF1001 // Internal EF Core API usage.
        {
            var ownerTypeTable = ownership.PrincipalEntityType.GetTableName();
            name = ownerTypeTable != null
                ? $"{ownerTypeTable}_{ownership.PrincipalToDependent.Name}"
                : $"{ownership.PrincipalToDependent.Name}_{name}";
        }

        return(truncate
            ? Uniquifier.Truncate(name, entityType.Model.GetMaxIdentifierLength())
            : name);
    }
Exemple #3
0
        /// <summary>
        ///     Returns the default name that would be used for this index.
        /// </summary>
        /// <param name="index"> The index. </param>
        /// <param name="storeObject"> The identifier of the store object. </param>
        /// <returns> The default name that would be used for this index. </returns>
        public static string GetDefaultDatabaseName([NotNull] this IIndex index, StoreObjectIdentifier storeObject)
        {
            var propertyNames = index.Properties.Select(p => p.GetColumnName(storeObject)).ToList();
            var rootIndex     = index;

            // Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
            // Using a hashset is detrimental to the perf when there are no cycles
            for (var i = 0; i < Metadata.Internal.RelationalEntityTypeExtensions.MaxEntityTypesSharingTable; i++)
            {
                var linkedIndex = rootIndex.DeclaringEntityType
                                  .FindRowInternalForeignKeys(storeObject)
                                  .SelectMany(fk => fk.PrincipalEntityType.GetIndexes())
                                  .FirstOrDefault(i => i.Properties.Select(p => p.GetColumnName(storeObject)).SequenceEqual(propertyNames));
                if (linkedIndex == null)
                {
                    break;
                }

                rootIndex = linkedIndex;
            }

            if (rootIndex != index)
            {
                return(rootIndex.GetDatabaseName(storeObject));
            }

            var baseName = new StringBuilder()
                           .Append("IX_")
                           .Append(storeObject.Name)
                           .Append("_")
                           .AppendJoin(propertyNames, "_")
                           .ToString();

            return(Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
        private static string GetDefaultName(IIndex index)
        {
            var baseName = new StringBuilder()
                           .Append(index.DeclaringEntityType.GetTableName())
                           .Append("_ix_")
                           .AppendJoin("_", index.Properties.Select(p => p.GetColumnName()))
                           .ToString();

            return(Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
    /// <summary>
    ///     Returns the default name that would be used for this index.
    /// </summary>
    /// <param name="index">The index.</param>
    /// <returns>The default name that would be used for this index.</returns>
    public static string GetDefaultDatabaseName(this IReadOnlyIndex index)
    {
        var tableName = index.DeclaringEntityType.GetTableName();
        var baseName  = new StringBuilder()
                        .Append("IX_")
                        .Append(tableName)
                        .Append('_')
                        .AppendJoin(index.Properties.Select(p => p.GetColumnBaseName()), "_")
                        .ToString();

        return(Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength()));
    }
        /// <summary>
        ///     Returns the default column name to which the property would be mapped.
        /// </summary>
        /// <param name="property"> The property. </param>
        /// <returns> The default column name to which the property would be mapped. </returns>
        public static string GetDefaultColumnName([NotNull] this IProperty property)
        {
            var sharedTablePrincipalPrimaryKeyProperty = property.FindSharedTableRootPrimaryKeyProperty();

            if (sharedTablePrincipalPrimaryKeyProperty != null)
            {
                return(sharedTablePrincipalPrimaryKeyProperty.GetColumnName());
            }

            var           entityType = property.DeclaringEntityType;
            StringBuilder builder    = null;

            do
            {
                var ownership = entityType.GetForeignKeys().SingleOrDefault(fk => fk.IsOwnership);
                if (ownership == null)
                {
                    entityType = null;
                }
                else
                {
                    var ownerType = ownership.PrincipalEntityType;
                    var table     = entityType.GetTableName();
                    if (table != null &&
                        table == ownerType.GetTableName() &&
                        entityType.GetSchema() == ownerType.GetSchema())
                    {
                        if (builder == null)
                        {
                            builder = new StringBuilder();
                        }

                        builder.Insert(0, "_");
                        builder.Insert(0, ownership.PrincipalToDependent.Name);
                        entityType = ownerType;
                    }
                    else
                    {
                        entityType = null;
                    }
                }
            }while (entityType != null);

            var baseName = property.Name;

            if (builder != null)
            {
                builder.Append(baseName);
                baseName = builder.ToString();
            }

            return(Uniquifier.Truncate(baseName, property.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
Exemple #7
0
        /// <summary>
        ///     Returns the default name that would be used for this index.
        /// </summary>
        /// <param name="index"> The index. </param>
        /// <returns> The default name that would be used for this index. </returns>
        public static string GetDefaultName([NotNull] this IIndex index)
        {
            var tableName = index.DeclaringEntityType.GetTableName();
            var schema    = index.DeclaringEntityType.GetSchema();
            var baseName  = new StringBuilder()
                            .Append("IX_")
                            .Append(tableName)
                            .Append("_")
                            .AppendJoin(index.Properties.Select(p => p.GetColumnName(tableName, schema)), "_")
                            .ToString();

            return(Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
Exemple #8
0
        /// <summary>
        ///     Returns the default constraint name that would be used for this foreign key.
        /// </summary>
        /// <param name="foreignKey"> The foreign key. </param>
        /// <returns> The default constraint name that would be used for this foreign key. </returns>
        public static string GetDefaultName([NotNull] this IForeignKey foreignKey)
        {
            var baseName = new StringBuilder()
                           .Append("FK_")
                           .Append(foreignKey.DeclaringEntityType.GetTableName())
                           .Append("_")
                           .Append(foreignKey.PrincipalEntityType.GetTableName())
                           .Append("_")
                           .AppendJoin(foreignKey.Properties.Select(p => p.GetColumnName()), "_")
                           .ToString();

            return(Uniquifier.Truncate(baseName, foreignKey.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
        private static string GetDefaultName(IMutableForeignKey key)
        {
            var baseName = new StringBuilder()
                           .Append(key.DeclaringEntityType.GetTableName())
                           .Append("_")
                           .Append(key.PrincipalEntityType.GetTableName())
                           .Append("_")
                           .AppendJoin("_", key.PrincipalKey.Properties.Select(p => p.GetColumnName()))
                           .Append("_fkey")
                           .ToString();

            return(Uniquifier.Truncate(baseName, key.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
Exemple #10
0
        /// <summary>
        ///     Returns the default table name that would be used for this entity type.
        /// </summary>
        /// <param name="entityType"> The entity type to get the table name for. </param>
        /// <returns> The default name of the table to which the entity type would be mapped. </returns>
        public static string GetDefaultTableName([NotNull] this IEntityType entityType)
        {
            var ownership = entityType.FindOwnership();

            if (ownership != null &&
                ownership.IsUnique)
            {
                return(ownership.PrincipalEntityType.GetTableName());
            }

            return(Uniquifier.Truncate(
                       entityType.HasDefiningNavigation()
                    ? $"{entityType.DefiningEntityType.GetTableName()}_{entityType.DefiningNavigationName}"
                    : entityType.ShortName(),
                       entityType.Model.GetMaxIdentifierLength()));
        }
        /// <summary>
        ///     Returns the default constraint name that would be used for this foreign key.
        /// </summary>
        /// <param name="foreignKey"> The foreign key. </param>
        /// <param name="tableName"> The table name. </param>
        /// <param name="schema"> The schema. </param>
        /// <param name="principalTableName"> The principal table name. </param>
        /// <param name="principalSchema"> The principal schema. </param>
        /// <returns> The default constraint name that would be used for this foreign key. </returns>
        public static string GetDefaultName(
            [NotNull] this IForeignKey foreignKey,
            [NotNull] string tableName,
            [CanBeNull] string schema,
            [NotNull] string principalTableName,
            [CanBeNull] string principalSchema)
        {
            var storeObject            = StoreObjectIdentifier.Table(tableName, schema);
            var propertyNames          = foreignKey.Properties.Select(p => p.GetColumnName(storeObject)).ToList();
            var principalPropertyNames = foreignKey.PrincipalKey.Properties.Select(p => p.GetColumnName(storeObject)).ToList();
            var rootForeignKey         = foreignKey;

            // Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
            // Using a hashset is detrimental to the perf when there are no cycles
            for (var i = 0; i < Metadata.Internal.RelationalEntityTypeExtensions.MaxEntityTypesSharingTable; i++)
            {
                var linkedForeignKey = rootForeignKey.DeclaringEntityType
                                       .FindRowInternalForeignKeys(storeObject)
                                       .SelectMany(fk => fk.PrincipalEntityType.GetForeignKeys())
                                       .FirstOrDefault(k => principalTableName == k.PrincipalEntityType.GetTableName() &&
                                                       principalSchema == k.PrincipalEntityType.GetSchema() &&
                                                       propertyNames.SequenceEqual(k.Properties.Select(p => p.GetColumnName(storeObject))) &&
                                                       principalPropertyNames.SequenceEqual(k.PrincipalKey.Properties.Select(p => p.GetColumnName(storeObject))));
                if (linkedForeignKey == null)
                {
                    break;
                }

                rootForeignKey = linkedForeignKey;
            }

            if (rootForeignKey != foreignKey)
            {
                return(rootForeignKey.GetConstraintName(tableName, schema, principalTableName, principalSchema));
            }

            var baseName = new StringBuilder()
                           .Append("FK_")
                           .Append(tableName)
                           .Append("_")
                           .Append(principalTableName)
                           .Append("_")
                           .AppendJoin(foreignKey.Properties.Select(p => p.GetColumnName(storeObject)), "_")
                           .ToString();

            return(Uniquifier.Truncate(baseName, foreignKey.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
        /// <summary>
        ///     Returns the default key constraint name that would be used for this key.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <returns> The default key constraint name that would be used for this key. </returns>
        public static string GetDefaultName([NotNull] this IKey key)
        {
            string name      = null;
            var    tableName = key.DeclaringEntityType.GetTableName();

            if (key.IsPrimaryKey())
            {
                name = "PK_" + tableName;
            }
            else
            {
                name = new StringBuilder()
                       .Append("AK_")
                       .Append(tableName)
                       .Append("_")
                       .AppendJoin(key.Properties.Select(p => p.GetColumnName()), "_")
                       .ToString();
            }

            return(Uniquifier.Truncate(name, key.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }
        /// <summary>
        ///     Returns the default table name that would be used for this entity type.
        /// </summary>
        /// <param name="entityType"> The entity type to get the table name for. </param>
        /// <param name="truncate"> A value indicating whether the name should be truncated to the max identifier length. </param>
        /// <returns> The default name of the table to which the entity type would be mapped. </returns>
        public static string GetDefaultTableName([NotNull] this IEntityType entityType, bool truncate = true)
        {
            var ownership = entityType.FindOwnership();

            if (ownership != null &&
                ownership.IsUnique)
            {
                return(ownership.PrincipalEntityType.GetTableName());
            }

            var name = entityType.ShortName();

            if (entityType.HasDefiningNavigation())
            {
                var definingTypeName = entityType.DefiningEntityType.GetTableName();
                name = definingTypeName != null
                    ? $"{definingTypeName}_{entityType.DefiningNavigationName}"
                    : $"{entityType.DefiningNavigationName}_{name}";
            }

            return(truncate
                ? Uniquifier.Truncate(name, entityType.Model.GetMaxIdentifierLength())
                : name);
        }
        /// <summary>
        ///     Returns the default key constraint name that would be used for this key for a particular table.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <param name="storeObject"> The identifier of the containing store object. </param>
        /// <returns> The default key constraint name that would be used for this key. </returns>
        public static string GetDefaultName([NotNull] this IKey key, StoreObjectIdentifier storeObject)
        {
            string name = null;

            if (key.IsPrimaryKey())
            {
                var rootKey = key;
                // Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
                // Using a hashset is detrimental to the perf when there are no cycles
                for (var i = 0; i < Metadata.Internal.RelationalEntityTypeExtensions.MaxEntityTypesSharingTable; i++)
                {
                    var linkingFk = rootKey.DeclaringEntityType.FindRowInternalForeignKeys(storeObject)
                                    .FirstOrDefault();
                    if (linkingFk == null)
                    {
                        break;
                    }

                    rootKey = linkingFk.PrincipalEntityType.FindPrimaryKey();
                }

                if (rootKey != null &&
                    rootKey != key)
                {
                    return(rootKey.GetName(storeObject));
                }

                name = "PK_" + storeObject.Name;
            }
            else
            {
                var propertyNames = key.Properties.Select(p => p.GetColumnName(storeObject)).ToList();
                var rootKey       = key;

                // Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
                // Using a hashset is detrimental to the perf when there are no cycles
                for (var i = 0; i < Metadata.Internal.RelationalEntityTypeExtensions.MaxEntityTypesSharingTable; i++)
                {
                    var linkedKey = rootKey.DeclaringEntityType
                                    .FindRowInternalForeignKeys(storeObject)
                                    .SelectMany(fk => fk.PrincipalEntityType.GetKeys())
                                    .FirstOrDefault(k => k.Properties.Select(p => p.GetColumnName(storeObject)).SequenceEqual(propertyNames));
                    if (linkedKey == null)
                    {
                        break;
                    }

                    rootKey = linkedKey;
                }

                if (rootKey != key)
                {
                    return(rootKey.GetName(storeObject));
                }

                name = new StringBuilder()
                       .Append("AK_")
                       .Append(storeObject.Name)
                       .Append("_")
                       .AppendJoin(key.Properties.Select(p => p.GetColumnName(storeObject)), "_")
                       .ToString();
            }

            return(Uniquifier.Truncate(name, key.DeclaringEntityType.Model.GetMaxIdentifierLength()));
        }