/// <summary> /// Marks the given entity type as shared, indicating that when discovered matching entity types /// should be configured as shared type entity type. /// </summary> /// <param name="model"> The model to add the shared type to. </param> /// <param name="type"> The type of the entity type that should be shared. </param> /// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param> public static void AddShared([NotNull] this IConventionModel model, [NotNull] Type type, bool fromDataAnnotation = false) => Check.NotNull((Model)model, nameof(model)).AddShared( Check.NotNull(type, nameof(type)), fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);
/// <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 IModel AddRelationalModel([NotNull] IConventionModel model) { var tables = new SortedDictionary <(string, string), Table>(); var views = new SortedDictionary <(string, string), View>(); foreach (var entityType in model.GetEntityTypes()) { var tableName = entityType.GetTableName(); var viewName = entityType.GetViewName(); if (tableName != null && viewName == null) { var schema = entityType.GetSchema(); if (!tables.TryGetValue((tableName, schema), out var table)) { table = new Table(tableName, schema); tables.Add((tableName, schema), table); } table.IsMigratable = table.IsMigratable || entityType.FindAnnotation(RelationalAnnotationNames.ViewDefinition) == null; var tableMapping = new TableMapping(entityType, table, includesDerivedTypes: true); foreach (var property in entityType.GetDeclaredProperties()) { var typeMapping = property.FindRelationalTypeMapping(); var columnName = property.GetColumnName(); var column = (Column)table.FindColumn(columnName); if (column == null) { column = new Column(columnName, property.GetColumnType() ?? typeMapping?.StoreType, table); column.IsNullable = property.IsColumnNullable(); table.Columns.Add(columnName, column); } else if (!property.IsColumnNullable()) { column.IsNullable = false; } var columnMapping = new ColumnMapping(property, column, typeMapping, tableMapping); tableMapping.ColumnMappings.Add(columnMapping); column.PropertyMappings.Add(columnMapping); var columnMappings = property[RelationalAnnotationNames.TableColumnMappings] as SortedSet <ColumnMapping>; if (columnMappings == null) { columnMappings = new SortedSet <ColumnMapping>(ColumnMappingComparer.Instance); property.SetAnnotation(RelationalAnnotationNames.TableColumnMappings, columnMappings); } columnMappings.Add(columnMapping); } var tableMappings = entityType[RelationalAnnotationNames.TableMappings] as SortedSet <TableMapping>; if (tableMappings == null) { tableMappings = new SortedSet <TableMapping>(TableMappingComparer.Instance); entityType.SetAnnotation(RelationalAnnotationNames.TableMappings, tableMappings); } tableMappings.Add(tableMapping); table.EntityTypeMappings.Add(tableMapping); } if (viewName != null) { var schema = entityType.GetSchema(); if (!views.TryGetValue((viewName, schema), out var view)) { view = new View(viewName, schema); views.Add((viewName, schema), view); } var viewMapping = new ViewMapping(entityType, view, includesDerivedTypes: true); foreach (var property in entityType.GetDeclaredProperties()) { var typeMapping = property.FindRelationalTypeMapping(); var columnName = property.GetColumnName(); var column = (ViewColumn)view.FindColumn(columnName); if (column == null) { column = new ViewColumn(columnName, property.GetColumnType() ?? typeMapping.StoreType, view); column.IsNullable = property.IsColumnNullable(); view.Columns.Add(columnName, column); } else if (!property.IsColumnNullable()) { column.IsNullable = false; } var columnMapping = new ViewColumnMapping(property, column, typeMapping, viewMapping); viewMapping.ColumnMappings.Add(columnMapping); column.PropertyMappings.Add(columnMapping); var columnMappings = property[RelationalAnnotationNames.ViewColumnMappings] as SortedSet <ViewColumnMapping>; if (columnMappings == null) { columnMappings = new SortedSet <ViewColumnMapping>(ViewColumnMappingComparer.Instance); property.SetAnnotation(RelationalAnnotationNames.ViewColumnMappings, columnMappings); } columnMappings.Add(columnMapping); } var tableMappings = entityType[RelationalAnnotationNames.ViewMappings] as SortedSet <ViewMapping>; if (tableMappings == null) { tableMappings = new SortedSet <ViewMapping>(ViewMappingComparer.Instance); entityType.SetAnnotation(RelationalAnnotationNames.ViewMappings, tableMappings); } tableMappings.Add(viewMapping); view.EntityTypeMappings.Add(viewMapping); } } if (tables.Any()) { foreach (var table in tables.Values) { PopulateInternalForeignKeys(table); } model.SetAnnotation(RelationalAnnotationNames.Tables, tables); } if (views.Any()) { foreach (var view in views.Values) { PopulateInternalForeignKeys(view); } model.SetAnnotation(RelationalAnnotationNames.Views, views); } return(model); }
/// <summary> /// Returns the configuration source for <see cref="ModelExtensions.GetPropertyAccessMode" />. /// </summary> /// <param name="model"> The model to find configuration source for. </param> /// <returns> The configuration source for <see cref="ModelExtensions.GetPropertyAccessMode" />. </returns> public static ConfigurationSource?GetPropertyAccessModeConfigurationSource([NotNull] this IConventionModel model) => model.FindAnnotation(CoreAnnotationNames.PropertyAccessMode)?.GetConfigurationSource();
/// <summary> /// Removes the function that is mapped to the method represented by the given /// <see cref="MethodInfo" />. /// </summary> /// <param name="model">The model to find the function in.</param> /// <param name="name">The model name of the function.</param> /// <returns>The removed function or <see langword="null" /> if the method is not mapped.</returns> public static IConventionDbFunction?RemoveDbFunction(this IConventionModel model, string name) => (IConventionDbFunction?)((IMutableModel)model).RemoveDbFunction(name);
/// <summary> /// Returns the configuration source for the collation. /// </summary> /// <param name="model">The model to find configuration source for.</param> /// <returns>The configuration source for the collation.</returns> public static ConfigurationSource?GetCollationConfigurationSource(this IConventionModel model) => model.FindAnnotation(RelationalAnnotationNames.Collation)?.GetConfigurationSource();
/// <summary> /// Returns all sequences contained in the model. /// </summary> /// <param name="model">The model to get the sequences in.</param> public static IEnumerable <IConventionSequence> GetSequences(this IConventionModel model) => Sequence.GetSequences(model).Cast <IConventionSequence>();
/// <summary> /// Finds a function that is mapped to the method represented by the given name. /// </summary> /// <param name="model">The model to find the function in.</param> /// <param name="name">The model name of the function.</param> /// <returns>The function or <see langword="null" /> if the method is not mapped.</returns> public static IConventionDbFunction?FindDbFunction(this IConventionModel model, string name) => (IConventionDbFunction?)((IReadOnlyModel)model).FindDbFunction(name);
/// <summary> /// Returns the <see cref="ConfigurationSource" /> for the default <see cref="NpgsqlValueGenerationStrategy" />. /// </summary> /// <param name="model"> The model. </param> /// <returns> The <see cref="ConfigurationSource" /> for the default <see cref="NpgsqlValueGenerationStrategy" />. </returns> public static ConfigurationSource?GetValueGenerationStrategyConfigurationSource(this IConventionModel model) => model.FindAnnotation(NpgsqlAnnotationNames.ValueGenerationStrategy)?.GetConfigurationSource();
public static PostgresExtension GetOrAddPostgresExtension( this IConventionModel model, string?schema, string name, string?version) => PostgresExtension.GetOrAddPostgresExtension(model, schema, name, version);
/// <summary> /// Sets the default container name. /// </summary> /// <param name="model"> The model. </param> /// <param name="name"> The name to set. </param> /// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param> public static void SetCosmosDefaultContainer([NotNull] this IConventionModel model, [CanBeNull] string name, bool fromDataAnnotation = false) => model.SetOrRemoveAnnotation( CosmosAnnotationNames.ContainerName, Check.NullButNotEmpty(name, nameof(name)), fromDataAnnotation);
/// <summary> /// Returns the configuration source for the default container name. /// </summary> /// <param name="model"> The model. </param> /// <returns> The configuration source for the default container name.</returns> public static ConfigurationSource?GetCosmosDefaultContainerConfigurationSource([NotNull] this IConventionModel model) => model.FindAnnotation(CosmosAnnotationNames.ContainerName)?.GetConfigurationSource();
public static IReadOnlyCollection <IConventionEntityType> GetEntityTypes( [NotNull] this IConventionModel model, [NotNull] string name) => ((Model)model).GetEntityTypes(name);
/// <summary> /// Gets the entity type for the given name, defining navigation name /// and the defining entity type. Returns <see langword="null" /> if no matching entity type is found. /// </summary> /// <param name="model"> The model to find the entity type in. </param> /// <param name="type"> The type of the entity type to find. </param> /// <param name="definingNavigationName"> The defining navigation of the entity type to find. </param> /// <param name="definingEntityType"> The defining entity type of the entity type to find. </param> /// <returns> The entity type, or <see langword="null" /> if none are found. </returns> public static IConventionEntityType FindEntityType( [NotNull] this IConventionModel model, [NotNull] Type type, [NotNull] string definingNavigationName, [NotNull] IConventionEntityType definingEntityType) => (IConventionEntityType)((IModel)model).FindEntityType(type, definingNavigationName, definingEntityType);
/// <summary> /// Forces post-processing on the model such that it is ready for use by the runtime. This post /// processing happens automatically when using <see cref="DbContext.OnModelCreating" />; this method allows it to be run /// explicitly in cases where the automatic execution is not possible. /// </summary> /// <param name="model"> The model to finalize. </param> /// <returns> The finalized <see cref="IModel" />. </returns> public static IModel FinalizeModel([NotNull] this IConventionModel model) => ((Model)model).FinalizeModel();
/// <summary> /// Finds a sequence with the given name. /// </summary> /// <param name="model">The model to find the sequence in.</param> /// <param name="name">The sequence name.</param> /// <param name="schema">The schema that contains the sequence.</param> /// <returns> /// The sequence or <see langword="null" /> if no sequence with the given name in /// the given schema was found. /// </returns> public static IConventionSequence?FindSequence( this IConventionModel model, string name, string?schema = null) => (IConventionSequence?)((IReadOnlyModel)model).FindSequence(name, schema);
public static ConfigurationSource?GetDatabaseTemplateConfigurationSource(this IConventionModel model) => model.FindAnnotation(NpgsqlAnnotationNames.DatabaseTemplate)?.GetConfigurationSource();
/// <summary> /// Removes the <see cref="IConventionSequence" /> with the given name. /// </summary> /// <param name="model">The model to find the sequence in.</param> /// <param name="name">The sequence name.</param> /// <param name="schema">The schema that contains the sequence.</param> /// <returns> /// The removed <see cref="IConventionSequence" /> or <see langword="null" /> if no sequence with the given name in /// the given schema was found. /// </returns> public static IConventionSequence?RemoveSequence( this IConventionModel model, string name, string?schema = null) => Sequence.RemoveSequence((IMutableModel)model, name, schema);
public static ConfigurationSource?GetTablespaceConfigurationSource(this IConventionModel model) => model.FindAnnotation(NpgsqlAnnotationNames.Tablespace)?.GetConfigurationSource();
/// <summary> /// Finds a function that is mapped to the method represented by the given <see cref="MethodInfo" />. /// </summary> /// <param name="model">The model to find the function in.</param> /// <param name="method">The <see cref="MethodInfo" /> for the method that is mapped to the function.</param> /// <returns>The function or <see langword="null" /> if the method is not mapped.</returns> public static IConventionDbFunction?FindDbFunction(this IConventionModel model, MethodInfo method) => (IConventionDbFunction?)((IReadOnlyModel)model).FindDbFunction(method);
public static string?SetDefaultColumnCollation(this IConventionModel model, string?collation, bool fromDataAnnotation = false) { model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.DefaultColumnCollation, collation, fromDataAnnotation); return(collation); }
/// <summary> /// Removes the function that is mapped to the method represented by the given /// <see cref="MethodInfo" />. /// </summary> /// <param name="model">The model to find the function in.</param> /// <param name="method">The <see cref="MethodInfo" /> for the method that is mapped to the function.</param> /// <returns>The removed function or <see langword="null" /> if the method is not mapped.</returns> public static IConventionDbFunction?RemoveDbFunction(this IConventionModel model, MethodInfo method) => (IConventionDbFunction?)((IMutableModel)model).RemoveDbFunction(method);
public static ConfigurationSource?GetDefaultColumnCollationConfigurationSource(this IConventionModel model) => model.FindAnnotation(NpgsqlAnnotationNames.DefaultColumnCollation)?.GetConfigurationSource();
/// <summary> /// Returns all functions contained in the model. /// </summary> /// <param name="model">The model to get the functions in.</param> public static IEnumerable <IConventionDbFunction> GetDbFunctions(this IConventionModel model) => DbFunction.GetDbFunctions(model).Cast <IConventionDbFunction>();
/// <summary> /// Returns the <see cref="ConfigurationSource" /> for the default hi-lo sequence schema. /// </summary> /// <param name="model"> The model. </param> /// <returns> The <see cref="ConfigurationSource" /> for the default hi-lo sequence schema. </returns> public static ConfigurationSource?GetHiLoSequenceSchemaConfigurationSource(this IConventionModel model) => model.FindAnnotation(NpgsqlAnnotationNames.HiLoSequenceSchema)?.GetConfigurationSource();
/// <summary> /// Returns the configuration source for the default schema. /// </summary> /// <param name="model">The model to find configuration source for.</param> /// <returns>The configuration source for the default schema.</returns> public static ConfigurationSource?GetDefaultSchemaConfigurationSource(this IConventionModel model) => model.FindAnnotation(RelationalAnnotationNames.DefaultSchema)?.GetConfigurationSource();
/// <summary> /// Sets the maximum length allowed for store identifiers. /// </summary> /// <param name="model">The model to set the default schema for.</param> /// <param name="length">The value to set.</param> /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> /// <returns>The configured value.</returns> public static int?SetMaxIdentifierLength(this IConventionModel model, int?length, bool fromDataAnnotation = false) { model.SetOrRemoveAnnotation(RelationalAnnotationNames.MaxIdentifierLength, length, fromDataAnnotation); return(length); }
/// <summary> /// Returns the entity types corresponding to the least derived types from the given. /// </summary> /// <param name="model"> The model to find the entity types in. </param> /// <param name="type"> The base type. </param> /// <param name="condition"> An optional condition for filtering entity types. </param> /// <returns> List of entity types corresponding to the least derived types from the given. </returns> public static IReadOnlyList <IConventionEntityType> FindLeastDerivedEntityTypes( [NotNull] this IConventionModel model, [NotNull] Type type, [CanBeNull] Func <IConventionEntityType, bool> condition = null) => Check.NotNull((Model)model, nameof(model)) .FindLeastDerivedEntityTypes(type, condition);
/// <summary> /// Returns the configuration source for <see cref="GetMaxIdentifierLength" />. /// </summary> /// <param name="model">The model to find configuration source for.</param> /// <returns>The configuration source for <see cref="GetMaxIdentifierLength" />.</returns> public static ConfigurationSource?GetMaxIdentifierLengthConfigurationSource(this IConventionModel model) => model.FindAnnotation(RelationalAnnotationNames.MaxIdentifierLength)?.GetConfigurationSource();
/// <summary> /// Returns the configuration source for <see cref="ModelExtensions.GetChangeTrackingStrategy" />. /// </summary> /// <param name="model"> The model to find configuration source for. </param> /// <returns> The configuration source for <see cref="ModelExtensions.GetChangeTrackingStrategy" />. </returns> public static ConfigurationSource?GetChangeTrackingStrategyConfigurationSource([NotNull] this IConventionModel model) => model.FindAnnotation(CoreAnnotationNames.ChangeTrackingStrategy)?.GetConfigurationSource();
/// <summary> /// Removes the given owned type, indicating that when discovered matching entity types /// should not be configured as owned. /// </summary> /// <param name="model"> The model to remove the owned type name from. </param> /// <param name="type"> The type of the entity type that should not be owned. </param> /// <returns> The name of the removed owned type. </returns> public static string RemoveOwned([NotNull] this IConventionModel model, [NotNull] Type type) => Check.NotNull((Model)model, nameof(model)).RemoveOwned( Check.NotNull(type, nameof(type)));