/// <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].FindSharedTableRootPrimaryKeyProperty(); if (sharedTablePrincipalPrimaryKeyProperty != null) { return(sharedTablePrincipalPrimaryKeyProperty.FindContainingPrimaryKey().GetName()); } var builder = new StringBuilder(); var tableName = key.DeclaringEntityType.GetTableName(); if (key.IsPrimaryKey()) { builder .Append("PK_") .Append(tableName); } else { builder .Append("AK_") .Append(tableName) .Append("_") .AppendJoin(key.Properties.Select(p => p.GetColumnName()), "_"); } return(IdentifierHelpers.Truncate(builder.ToString(), key.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 GetDefaultName([NotNull] this IIndex index) { var baseName = new StringBuilder() .Append("IX_") .Append(index.DeclaringEntityType.GetTableName()) .Append("_") .AppendJoin(index.Properties.Select(p => p.GetColumnName()), "_") .ToString(); return(IdentifierHelpers.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength())); }
/// <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(IdentifierHelpers.Truncate(baseName, foreignKey.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; if (entityType.GetTableName() == 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(property.Name); baseName = builder.ToString(); } return(IdentifierHelpers.Truncate(baseName, property.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> /// <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(IdentifierHelpers.Truncate( entityType.HasDefiningNavigation() ? $"{entityType.DefiningEntityType.GetTableName()}_{entityType.DefiningNavigationName}" : entityType.ShortName(), entityType.Model.GetMaxIdentifierLength())); }