/// <summary> /// Gets a value indicating whether the entity type is ignored by Migrations. /// </summary> /// <param name="entityType">The entity type.</param> /// <returns>A value indicating whether the entity type is ignored by Migrations.</returns> public static bool IsIgnoredByMigrations([NotNull] this IEntityType entityType) { if (entityType.BaseType != null && entityType.BaseType.IsIgnoredByMigrations()) { return(true); } if (entityType.GetTableName() != null) { return(false); } var viewDefinition = entityType.FindAnnotation(RelationalAnnotationNames.ViewDefinition); if (viewDefinition?.Value != null) { return(false); } var ownership = entityType.FindOwnership(); if (ownership != null && ownership.IsUnique) { return(ownership.PrincipalEntityType.IsIgnoredByMigrations()); } return(true); }
private IEntityType FindSameTabledPrincipalType(IEntityType dependentType) { var tableId = GetRelationalId(dependentType); if (dependentType.FindOwnership() is IForeignKey ownership) { if (tableId.Equals(GetRelationalId(ownership.PrincipalEntityType))) { return(ownership.PrincipalEntityType); } else { return(null); } } foreach (var foreignKey in dependentType.GetForeignKeys()) { var principalType = foreignKey.PrincipalEntityType; if (tableId.Equals(GetRelationalId(principalType)) && principalType != principalType.RootType() && principalType.RootType() != dependentType.RootType()) { return(principalType); } } return(null); }
/// <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.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); }
private IQueryable BuildQueryRoot(IEntityType entityType) { return(entityType.DefiningEntityType is IEntityType definingEntityType ? BuildQueryRoot(definingEntityType, entityType, entityType.DefiningNavigationName) : entityType.FindOwnership() is IForeignKey ownership ? BuildQueryRoot(ownership.PrincipalEntityType, entityType, ownership.PrincipalToDependent.Name) : (IQueryable)_setCache.GetOrAddSet(_setSource, entityType.ClrType)); }
/// <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([NotNull] this IEntityType 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([NotNull] this IEntityType 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([NotNull] this IEntityType entityType) { var ownership = entityType.FindOwnership(); if (ownership != null && ownership.IsUnique) { return(ownership.PrincipalEntityType.GetSchema()); } return(entityType.HasDefiningNavigation() ? entityType.DefiningEntityType.GetSchema() ?? entityType.Model.GetDefaultSchema() : entityType.Model.GetDefaultSchema()); }
/// <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 name of the view to which the entity type is mapped. /// </summary> /// <param name="entityType"> The entity type to get the view name for. </param> /// <returns> The name of the view to which the entity type is mapped. </returns> public static string GetViewName([NotNull] this IEntityType entityType) { if (entityType.BaseType != null) { return(entityType.GetRootType().GetViewName()); } if (entityType.FindAnnotation(RelationalAnnotationNames.ViewDefinition) != null) { return(entityType.GetTableName()); } var ownership = entityType.FindOwnership(); if (ownership != null && ownership.IsUnique) { return(ownership.PrincipalEntityType.GetViewName()); } return(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([NotNull] this IEntityType 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()); }
/// <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); }
public static bool IsOwnedInSameTableAsOwner(IEntityType owned) { var ownership = owned.FindOwnership(); if (ownership is null) { return(false); } var owner = ownership.PrincipalEntityType; var ownedTables = owned.GetTableMappings(); foreach (var ot in ownedTables) { var isSharingTable = ot.Table.EntityTypeMappings.Any(y => y.EntityType == owner); if (isSharingTable == false) { return(false); } } return(true); }
private static string GetDefaultContainingPropertyName(IEntityType entityType) => entityType.FindOwnership()?.PrincipalToDependent.Name;
private void AddConditions( SelectExpression selectExpression, IEntityType entityType, ICollection<IEntityType> sharingTypes = null, bool skipJoins = false) { if (entityType.FindPrimaryKey() == null) { AddDiscriminatorCondition(selectExpression, entityType); } else { sharingTypes ??= new HashSet<IEntityType>( entityType.Model.GetEntityTypes() .Where( et => et.FindPrimaryKey() != null && et.GetTableName() == entityType.GetTableName() && et.GetSchema() == entityType.GetSchema())); if (sharingTypes.Count > 0) { var discriminatorAdded = AddDiscriminatorCondition(selectExpression, entityType); var linkingFks = entityType.GetRootType().FindForeignKeys(entityType.FindPrimaryKey().Properties) .Where( fk => fk.PrincipalKey.IsPrimaryKey() && fk.PrincipalEntityType != entityType && sharingTypes.Contains(fk.PrincipalEntityType)) .ToList(); if (linkingFks.Count > 0) { if (!discriminatorAdded) { AddOptionalDependentConditions(selectExpression, entityType, sharingTypes); } if (!skipJoins) { var first = true; foreach (var foreignKey in linkingFks) { if (!(entityType.FindOwnership() == foreignKey && foreignKey.PrincipalEntityType.BaseType == null)) { var otherSelectExpression = first ? selectExpression : new SelectExpression(entityType); AddInnerJoin(otherSelectExpression, foreignKey, sharingTypes, skipInnerJoins: false); if (first) { first = false; } else { selectExpression.ApplyUnion(otherSelectExpression, distinct: true); } } } } } } } }
/// <summary> /// Generates code for an <see cref="IEntityType" />. /// </summary> /// <param name="builderName"> The name of the builder variable. </param> /// <param name="entityType"> The entity type. </param> /// <param name="stringBuilder"> The builder code is added to. </param> protected virtual void GenerateEntityType( [NotNull] string builderName, [NotNull] IEntityType entityType, [NotNull] IndentedStringBuilder stringBuilder) { Check.NotEmpty(builderName, nameof(builderName)); Check.NotNull(entityType, nameof(entityType)); Check.NotNull(stringBuilder, nameof(stringBuilder)); var ownership = entityType.FindOwnership(); var ownerNavigation = ownership?.PrincipalToDependent.Name; stringBuilder .Append(builderName) .Append( ownerNavigation != null ? ownership.IsUnique ? ".OwnsOne(" : ".OwnsMany(" : ".Entity(") .Append(Code.Literal(entityType.Name)); if (ownerNavigation != null) { stringBuilder .Append(", ") .Append(Code.Literal(ownerNavigation)); } if (builderName.StartsWith("b", StringComparison.Ordinal)) { // ReSharper disable once InlineOutVariableDeclaration var counter = 1; if (builderName.Length > 1 && int.TryParse(builderName.Substring(1, builderName.Length - 1), out counter)) { counter++; } builderName = "b" + (counter == 0 ? "" : counter.ToString()); } else { builderName = "b"; } stringBuilder .Append(", ") .Append(builderName) .AppendLine(" =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); using (stringBuilder.Indent()) { GenerateBaseType(builderName, entityType.BaseType, stringBuilder); GenerateProperties(builderName, entityType.GetDeclaredProperties(), stringBuilder); GenerateKeys(builderName, entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder); GenerateIndexes(builderName, entityType.GetDeclaredIndexes(), stringBuilder); GenerateEntityTypeAnnotations(builderName, entityType, stringBuilder); if (ownerNavigation != null) { GenerateRelationships(builderName, entityType, stringBuilder); } GenerateData(builderName, entityType.GetProperties(), entityType.GetData(providerValues: true), stringBuilder); } stringBuilder .AppendLine("});"); } }
protected override void GenerateEntityType( string builderName, IEntityType entityType, IndentedStringBuilder stringBuilder) { var isUserType = entityType.IsUserDefinedType(); var ownership = entityType.FindOwnership(); var ownerNavigation = ownership?.PrincipalToDependent.Name; stringBuilder .Append(builderName) .Append( ownerNavigation != null ? ownership.IsUnique ? ".OwnsOne(" : ".OwnsMany(" : ".Entity(") .Append(Code.Literal(entityType.Name)); if (ownerNavigation != null) { stringBuilder .Append(", ") .Append(ownerNavigation); } if (builderName.StartsWith("b", StringComparison.Ordinal)) { var counter = 1; if (builderName.Length > 1 && int.TryParse(builderName.Substring(1), out counter)) { counter++; } builderName = "b" + (counter == 0 ? "" : counter.ToString()); } else { builderName = "b"; } stringBuilder .Append(", ") .Append(builderName) .AppendLine(" =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); var properties = entityType.GetDeclaredProperties(); var assms = AppDomain.CurrentDomain.GetAssemblies(); using (stringBuilder.Indent()) { GenerateBaseType(builderName, entityType.BaseType, stringBuilder); GenerateProperties(builderName, entityType.GetDeclaredProperties().Where(p => CassandraMigrationsModelDiffer.CheckProperty(assms, p)), stringBuilder); GenerateKeys(builderName, entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder); GenerateEntityTypeAnnotations(builderName, entityType, stringBuilder); GenerateCheckConstraints(builderName, entityType, stringBuilder); if (ownerNavigation != null) { GenerateRelationships(builderName, entityType, stringBuilder); } GenerateData(builderName, entityType.GetProperties(), entityType.GetSeedData(providerValues: true), stringBuilder); } stringBuilder .AppendLine("});"); } }