/// <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);
    }
    /// <summary>
    ///     Returns the default view 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?GetDefaultViewName(this IReadOnlyEntityType entityType)
    {
        var ownership = entityType.FindOwnership();

        return(ownership != null &&
               ownership.IsUnique
                ? ownership.PrincipalEntityType.GetViewName()
                : null);
    }
    /// <summary>
    ///     Returns the default database schema that would be used for this entity view.
    /// </summary>
    /// <param name="entityType">The entity type to get the view schema for.</param>
    /// <returns>The default database schema to which the entity type would be mapped.</returns>
    public static string?GetDefaultViewSchema(this IReadOnlyEntityType entityType)
    {
        var ownership = entityType.FindOwnership();

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

        return(GetViewName(entityType) != null?entityType.Model.GetDefaultSchema() : null);
    }
    /// <summary>
    ///     Returns the default database schema that would be used for this entity type.
    /// </summary>
    /// <param name="entityType">The entity type to get the schema for.</param>
    /// <returns>The default database schema to which the entity type would be mapped.</returns>
    public static string?GetDefaultSchema(this IReadOnlyEntityType entityType)
    {
        var ownership = entityType.FindOwnership();

        if (ownership != null)
        {
            return(ownership.PrincipalEntityType.GetSchema());
        }

        var skipNavigationSchema = entityType.GetForeignKeys().SelectMany(fk => fk.GetReferencingSkipNavigations())
                                   .FirstOrDefault(n => !n.IsOnDependent)
                                   ?.DeclaringEntityType.GetSchema();

        if (skipNavigationSchema != null &&
            entityType.GetForeignKeys().SelectMany(fk => fk.GetReferencingSkipNavigations())
            .Where(n => !n.IsOnDependent)
            .All(n => n.DeclaringEntityType.GetSchema() == skipNavigationSchema))
        {
            return(skipNavigationSchema);
        }

        return(entityType.Model.GetDefaultSchema());
    }
 private static string?GetDefaultContainingPropertyName(IReadOnlyEntityType entityType)
 => entityType.FindOwnership()?.PrincipalToDependent !.Name;
 private static string?GetDefaultContainingPropertyName(IReadOnlyEntityType entityType)
 => entityType.FindOwnership() is IReadOnlyForeignKey ownership
 private static string?GetDefaultContainer(IReadOnlyEntityType entityType)
 => entityType.FindOwnership() != null
         ? null
         : (entityType.Model.GetDefaultContainer()
            ?? entityType.ShortName());
Пример #8
0
 /// <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 bool IsDocumentRoot(this IReadOnlyEntityType entityType)
 => entityType.BaseType?.IsDocumentRoot()
 ?? (entityType.FindOwnership() == null ||
     entityType[CosmosAnnotationNames.ContainerName] != null);