public override void ProcessPropertyAnnotationChanged(IConventionPropertyBuilder propertyBuilder, string name, IConventionAnnotation annotation, IConventionAnnotation oldAnnotation, IConventionContext <IConventionAnnotation> context) { if (name == FbAnnotationNames.ValueGenerationStrategy) { propertyBuilder.ValueGenerated(GetValueGenerated(propertyBuilder.Metadata)); return; } base.ProcessPropertyAnnotationChanged(propertyBuilder, name, annotation, oldAnnotation, context); }
void IProjectionProviderConvention.Complete(IConventionContext context) { Complete(context); }
/// <summary> /// Called after a model is initialized. /// </summary> /// <param name="modelBuilder"> The builder for the model. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessModelInitialized(IConventionModelBuilder modelBuilder, IConventionContext <IConventionModelBuilder> context) { var contextType = Dependencies.ContextType; while (contextType != null && contextType != typeof(DbContext)) { var functions = contextType.GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Where(mi => mi.IsDefined(typeof(DbFunctionAttribute))); foreach (var function in functions) { modelBuilder.HasDbFunction(function); } contextType = contextType.BaseType; } }
/// <summary> /// Called after a navigation property that has an attribute is added to an entity type. /// </summary> /// <param name="relationshipBuilder"> The builder for the relationship. </param> /// <param name="navigation"> The navigation. </param> /// <param name="attribute"> The attribute. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessNavigationAdded( [NotNull] IConventionRelationshipBuilder relationshipBuilder, [NotNull] IConventionNavigation navigation, [NotNull] TAttribute attribute, [NotNull] IConventionContext <IConventionNavigation> context) => throw new NotImplementedException();
void IProjectionProviderConvention.Initialize(IConventionContext context) { base.Initialize(context); }
/// <inheritdoc /> public virtual void ProcessEntityTypeAdded( IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext <IConventionEntityTypeBuilder> context) => TryConfigurePrimaryKey(entityTypeBuilder);
/// <inheritdoc /> public virtual void ProcessForeignKeyOwnershipChanged( IConventionForeignKeyBuilder relationshipBuilder, IConventionContext <bool?> context) { TryConfigurePrimaryKey(relationshipBuilder.Metadata.DeclaringEntityType.Builder); }
/// <summary> /// Called after a skip navigation property that has an attribute is added to an entity type. /// </summary> /// <param name="skipNavigationBuilder"> The builder for the navigation. </param> /// <param name="attribute"> The attribute. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessSkipNavigationAdded( [NotNull] IConventionSkipNavigationBuilder skipNavigationBuilder, [NotNull] TAttribute attribute, [NotNull] IConventionContext <IConventionSkipNavigationBuilder> context) => throw new NotImplementedException();
/// <summary> /// Called after the principal end of a foreign key is changed. /// </summary> /// <param name="relationshipBuilder"> The builder for the foreign key. </param> /// <param name="dependentToPrincipalAttributes"> The attributes on the dependent to principal navigation. </param> /// <param name="principalToDependentAttributes"> The attributes on the principal to dependent navigation. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessForeignKeyPrincipalEndChanged( [NotNull] IConventionForeignKeyBuilder relationshipBuilder, [CanBeNull] IEnumerable <TAttribute> dependentToPrincipalAttributes, [CanBeNull] IEnumerable <TAttribute> principalToDependentAttributes, [NotNull] IConventionContext <IConventionForeignKeyBuilder> context) => throw new NotImplementedException();
/// <summary> /// Called after a model is finalized. /// </summary> /// <param name="modelBuilder"> The builder for the model. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessModelFinalized(IConventionModelBuilder modelBuilder, IConventionContext <IConventionModelBuilder> context) { foreach (var property in modelBuilder.Metadata.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties())) { property.Builder.HasAnnotation( CoreAnnotationNames.TypeMapping, _typeMappingSource.FindMapping(property)); } }
/// <summary> /// Called after the backing field for a property is changed. /// </summary> /// <param name="propertyBuilder"> The builder for the property. </param> /// <param name="newFieldInfo"> The new field. </param> /// <param name="oldFieldInfo"> The old field. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessPropertyFieldChanged( IConventionPropertyBuilder propertyBuilder, FieldInfo newFieldInfo, FieldInfo oldFieldInfo, IConventionContext <FieldInfo> context) => TryConfigurePrimaryKey(propertyBuilder.Metadata.DeclaringEntityType.Builder);
void ISortProviderConvention.Complete(IConventionContext context) { Complete(context); }
void ISortProviderConvention.Initialize(IConventionContext context) { base.Initialize(context); }
/// <inheritdoc /> public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext <IConventionModelBuilder> context) { foreach (var function in modelBuilder.Metadata.GetDbFunctions()) { ProcessDbFunctionAdded(function.Builder, context); } }
/// <summary> /// Called after an annotation is changed on a property. /// </summary> /// <param name="propertyBuilder"> The builder for the property. </param> /// <param name="name"> The annotation name. </param> /// <param name="annotation"> The new annotation. </param> /// <param name="oldAnnotation"> The old annotation. </param> /// <param name="context"> Additional information associated with convention execution. </param> public override void ProcessPropertyAnnotationChanged( IConventionPropertyBuilder propertyBuilder, string name, IConventionAnnotation annotation, IConventionAnnotation oldAnnotation, IConventionContext <IConventionAnnotation> context) { if (annotation == null || oldAnnotation?.Value != null) { return; } var configurationSource = annotation.GetConfigurationSource(); var fromDataAnnotation = configurationSource != ConfigurationSource.Convention; switch (name) { case RelationalAnnotationNames.DefaultValue: if (propertyBuilder.HasValueGenerationStrategy(null, fromDataAnnotation) == null && propertyBuilder.HasDefaultValue(null, fromDataAnnotation) != null) { context.StopProcessing(); return; } break; case RelationalAnnotationNames.DefaultValueSql: if (propertyBuilder.HasValueGenerationStrategy(null, fromDataAnnotation) == null && propertyBuilder.HasDefaultValueSql(null, fromDataAnnotation) != null) { context.StopProcessing(); return; } break; case RelationalAnnotationNames.ComputedColumnSql: if (propertyBuilder.HasValueGenerationStrategy(null, fromDataAnnotation) == null && propertyBuilder.HasComputedColumnSql(null, fromDataAnnotation) != null) { context.StopProcessing(); return; } break; case SqlServerAnnotationNames.ValueGenerationStrategy: if ((propertyBuilder.HasDefaultValue(null, fromDataAnnotation) == null | propertyBuilder.HasDefaultValueSql(null, fromDataAnnotation) == null | propertyBuilder.HasComputedColumnSql(null, fromDataAnnotation) == null) && propertyBuilder.HasValueGenerationStrategy(null, fromDataAnnotation) != null) { context.StopProcessing(); return; } break; } base.ProcessPropertyAnnotationChanged(propertyBuilder, name, annotation, oldAnnotation, context); }
/// <inheritdoc /> public virtual void ProcessModelFinalizing( IConventionModelBuilder modelBuilder, IConventionContext <IConventionModelBuilder> context) { GetMappings(modelBuilder.Metadata, out var tableToEntityTypes, out var concurrencyColumnsToProperties); foreach (var tableToEntityType in tableToEntityTypes) { var table = tableToEntityType.Key; if (!concurrencyColumnsToProperties.TryGetValue(table, out var concurrencyColumns)) { continue; // this table has no mapped concurrency columns } var entityTypesMappedToTable = tableToEntityType.Value; foreach (var concurrencyColumn in concurrencyColumns) { var concurrencyColumnName = concurrencyColumn.Key; var propertiesMappedToConcurrencyColumn = concurrencyColumn.Value; var entityTypesMissingConcurrencyColumn = new Dictionary <IConventionEntityType, IConventionProperty>(); foreach (var entityType in entityTypesMappedToTable) { var foundMappedProperty = false; foreach (var mappedProperty in propertiesMappedToConcurrencyColumn) { var declaringEntityType = mappedProperty.DeclaringEntityType; if (declaringEntityType.IsAssignableFrom(entityType) || declaringEntityType.IsInOwnershipPath(entityType) || entityType.IsInOwnershipPath(declaringEntityType)) { foundMappedProperty = true; break; } } foundMappedProperty = foundMappedProperty || entityType.GetAllBaseTypes().SelectMany(t => t.GetDeclaredProperties()) .Any(p => p.GetColumnName(StoreObjectIdentifier.Table(table.Table, table.Schema)) == concurrencyColumnName); if (!foundMappedProperty) { // store the entity type which is missing the // concurrency token property, mapped to an example // property which _is_ mapped to this concurrency token // column and which will be used later as a template entityTypesMissingConcurrencyColumn.Add( entityType, propertiesMappedToConcurrencyColumn.First()); } } RemoveDerivedEntityTypes(ref entityTypesMissingConcurrencyColumn); foreach (var entityTypeToExampleProperty in entityTypesMissingConcurrencyColumn) { var entityType = entityTypeToExampleProperty.Key; var exampleProperty = entityTypeToExampleProperty.Value; var concurrencyShadowPropertyBuilder = #pragma warning disable EF1001 // Internal EF Core API usage. ((InternalEntityTypeBuilder)entityType.Builder).CreateUniqueProperty( ConcurrencyPropertyPrefix + exampleProperty.Name, exampleProperty.ClrType, !exampleProperty.IsNullable).Builder; concurrencyShadowPropertyBuilder .HasColumnName(concurrencyColumnName) .HasColumnType(exampleProperty.GetColumnType()) ?.IsConcurrencyToken(true) ?.ValueGenerated(exampleProperty.ValueGenerated); #pragma warning restore EF1001 // Internal EF Core API usage. } } } }
/// <summary> /// Called after an entity type is added to the model. /// </summary> /// <param name="entityTypeBuilder"> The builder for the entity type. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessEntityTypeAdded( IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext <IConventionEntityTypeBuilder> context) { Process(entityTypeBuilder); }
/// <summary> /// Called after an entity type is removed from the model. /// </summary> /// <param name="modelBuilder"> The builder for the model. </param> /// <param name="entityType"> The removed entity type. </param> /// <param name="context"> Additional information associated with convention execution. </param> public override void ProcessEntityTypeRemoved( IConventionModelBuilder modelBuilder, IConventionEntityType entityType, IConventionContext <IConventionEntityType> context) { }
/// <inheritdoc /> public virtual void ProcessPropertyAdded( IConventionPropertyBuilder propertyBuilder, IConventionContext <IConventionPropertyBuilder> context) { TryConfigurePrimaryKey(propertyBuilder.Metadata.DeclaringEntityType.Builder); }
/// <inheritdoc /> public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext <IConventionModelBuilder> context) { var definition = CoreResources.LogRedundantIndexRemoved(Dependencies.Logger); if (!Dependencies.Logger.ShouldLog(definition) && !Dependencies.Logger.DiagnosticSource.IsEnabled(definition.EventId.Name)) { return; } foreach (var entityType in modelBuilder.Metadata.GetEntityTypes()) { foreach (var declaredForeignKey in entityType.GetDeclaredForeignKeys()) { foreach (var key in entityType.GetKeys()) { if (AreIndexedBy( declaredForeignKey.Properties, declaredForeignKey.IsUnique, key.Properties, coveringIndexUnique: true)) { if (declaredForeignKey.Properties.Count != key.Properties.Count) { Dependencies.Logger.RedundantIndexRemoved(declaredForeignKey.Properties, key.Properties); } } } foreach (var existingIndex in entityType.GetIndexes()) { if (AreIndexedBy( declaredForeignKey.Properties, declaredForeignKey.IsUnique, existingIndex.Properties, existingIndex.IsUnique)) { if (declaredForeignKey.Properties.Count != existingIndex.Properties.Count) { Dependencies.Logger.RedundantIndexRemoved(declaredForeignKey.Properties, existingIndex.Properties); } } } } } }
protected internal new void Initialize(IConventionContext context) { base.Initialize(context); }
/// <summary> /// Called after a foreign key is removed. /// </summary> /// <param name="entityTypeBuilder"> The builder for the entity type. </param> /// <param name="foreignKey"> The removed foreign key. </param> /// <param name="context"> Additional information associated with convention execution. </param> public virtual void ProcessForeignKeyRemoved( IConventionEntityTypeBuilder entityTypeBuilder, IConventionForeignKey foreignKey, IConventionContext <IConventionForeignKey> context) { OnForeignKeyRemoved(foreignKey.DeclaringEntityType, foreignKey.Properties); }
void IFilterProviderConvention.Complete(IConventionContext context) { Complete(context); }
/// <summary> /// Called after an entity type is added to the model if it has an attribute. /// </summary> /// <param name="entityTypeBuilder"> The builder for the entity type. </param> /// <param name="attribute"> The attribute. </param> /// <param name="context"> Additional information associated with convention execution. </param> protected abstract void ProcessEntityTypeAdded( IConventionEntityTypeBuilder entityTypeBuilder, TAttribute attribute, IConventionContext <IConventionEntityTypeBuilder> context);
public new void Initialize(IConventionContext context) { base.Initialize(context); }
private IConventionForeignKeyBuilder?UpdateRelationshipBuilder( IConventionForeignKeyBuilder relationshipBuilder, IConventionContext context) { var foreignKey = relationshipBuilder.Metadata; var fkPropertyOnPrincipal = FindForeignKeyAttributeOnProperty(foreignKey.PrincipalEntityType, foreignKey.PrincipalToDependent?.Name); var fkPropertyOnDependent = FindForeignKeyAttributeOnProperty(foreignKey.DeclaringEntityType, foreignKey.DependentToPrincipal?.Name); if (fkPropertyOnDependent != null && fkPropertyOnPrincipal != null) { Dependencies.Logger.ForeignKeyAttributesOnBothPropertiesWarning( foreignKey.PrincipalToDependent !, foreignKey.DependentToPrincipal !, fkPropertyOnPrincipal, fkPropertyOnDependent); var newBuilder = SplitNavigationsToSeparateRelationships(relationshipBuilder); if (newBuilder is null) { context.StopProcessing(); return(null); } relationshipBuilder = newBuilder; fkPropertyOnPrincipal = null; } var fkPropertiesOnPrincipalToDependent = FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: false); var fkPropertiesOnDependentToPrincipal = FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: true); if (fkPropertiesOnDependentToPrincipal != null && fkPropertiesOnPrincipalToDependent != null) { Dependencies.Logger.ForeignKeyAttributesOnBothNavigationsWarning( relationshipBuilder.Metadata.DependentToPrincipal !, relationshipBuilder.Metadata.PrincipalToDependent !); var newBuilder = SplitNavigationsToSeparateRelationships(relationshipBuilder); if (newBuilder is null) { context.StopProcessing(); return(null); } relationshipBuilder = newBuilder; fkPropertiesOnPrincipalToDependent = null; } var fkPropertiesOnNavigation = fkPropertiesOnDependentToPrincipal ?? fkPropertiesOnPrincipalToDependent; var upgradePrincipalToDependentNavigationSource = fkPropertiesOnPrincipalToDependent != null; var upgradeDependentToPrincipalNavigationSource = fkPropertiesOnDependentToPrincipal != null; var shouldInvert = false; IReadOnlyList <string> fkPropertiesToSet; if (fkPropertiesOnNavigation == null || fkPropertiesOnNavigation.Count == 0) { if (fkPropertyOnDependent == null && fkPropertyOnPrincipal == null) { return(null); } if (fkPropertyOnDependent != null) { fkPropertiesToSet = new List <string> { fkPropertyOnDependent.GetSimpleMemberName() }; upgradeDependentToPrincipalNavigationSource = true; } else { if (foreignKey.PrincipalToDependent !.IsCollection) { context.StopProcessing(); return(null); } shouldInvert = true; fkPropertiesToSet = new List <string> { fkPropertyOnPrincipal !.GetSimpleMemberName() }; upgradePrincipalToDependentNavigationSource = true; } } else { fkPropertiesToSet = fkPropertiesOnNavigation; if (fkPropertyOnDependent == null && fkPropertyOnPrincipal == null) { if (fkPropertiesOnPrincipalToDependent != null && foreignKey.IsUnique) { shouldInvert = true; } } else { var fkProperty = fkPropertyOnDependent ?? fkPropertyOnPrincipal; if (fkPropertiesOnNavigation.Count != 1 || !Equals(fkPropertiesOnNavigation.First(), fkProperty !.GetSimpleMemberName())) { Dependencies.Logger.ConflictingForeignKeyAttributesOnNavigationAndPropertyWarning( fkPropertiesOnDependentToPrincipal != null ? relationshipBuilder.Metadata.DependentToPrincipal ! : relationshipBuilder.Metadata.PrincipalToDependent !, fkProperty !); var newBuilder = SplitNavigationsToSeparateRelationships(relationshipBuilder); if (newBuilder is null) { context.StopProcessing(); return(null); } relationshipBuilder = newBuilder; upgradePrincipalToDependentNavigationSource = false; fkPropertiesToSet = fkPropertiesOnDependentToPrincipal ?? new List <string> { fkPropertyOnDependent !.GetSimpleMemberName() }; } if (fkPropertyOnDependent != null) { upgradeDependentToPrincipalNavigationSource = true; } else { shouldInvert = true; } } } var newRelationshipBuilder = relationshipBuilder; if (upgradeDependentToPrincipalNavigationSource) { newRelationshipBuilder = newRelationshipBuilder.HasNavigation( newRelationshipBuilder.Metadata.DependentToPrincipal !.Name, pointsToPrincipal: true, fromDataAnnotation: true) !; } if (upgradePrincipalToDependentNavigationSource) { newRelationshipBuilder = newRelationshipBuilder.HasNavigation( newRelationshipBuilder.Metadata.PrincipalToDependent !.Name, pointsToPrincipal: false, fromDataAnnotation: true) !; } if (shouldInvert) { newRelationshipBuilder = newRelationshipBuilder.HasEntityTypes( foreignKey.DeclaringEntityType, foreignKey.PrincipalEntityType, fromDataAnnotation: true) !; } else { var existingProperties = foreignKey.DeclaringEntityType.FindProperties(fkPropertiesToSet); if (existingProperties != null) { var conflictingFk = foreignKey.DeclaringEntityType.FindForeignKeys(existingProperties) .FirstOrDefault( fk => fk != foreignKey && fk.PrincipalEntityType == foreignKey.PrincipalEntityType && fk.GetConfigurationSource() == ConfigurationSource.DataAnnotation && fk.GetPropertiesConfigurationSource() == ConfigurationSource.DataAnnotation); if (conflictingFk != null) { throw new InvalidOperationException( CoreStrings.ConflictingForeignKeyAttributes( existingProperties.Format(), foreignKey.DeclaringEntityType.DisplayName(), foreignKey.PrincipalEntityType.DisplayName())); } } } return(newRelationshipBuilder?.HasForeignKey(fkPropertiesToSet, fromDataAnnotation: true)); }
protected override void Complete(IConventionContext context) { if (Definition.Handlers.Count == 0) { throw ProjectionProvider_NoHandlersConfigured(this); } IServiceProvider services = new DictionaryServiceProvider( (typeof(IConventionContext), context), (typeof(IDescriptorContext), context.DescriptorContext), (typeof(ITypeInspector), context.DescriptorContext.TypeInspector)) .Include(context.Services); foreach ((Type type, IProjectionFieldHandler? instance) in Definition.Handlers) { switch (instance) { case null when services.TryGetOrCreateService( type, out IProjectionFieldHandler?service): _fieldHandlers.Add(service); break; case null: throw new SchemaException( ProjectionConvention_UnableToCreateFieldHandler(this, type)); default: _fieldHandlers.Add(instance); break; } } foreach ((Type? type, IProjectionFieldInterceptor? instance) in Definition.Interceptors) { switch (instance) { case null when services.TryGetOrCreateService( type, out IProjectionFieldInterceptor?service): _fieldInterceptors.Add(service); break; case null: throw new SchemaException( ProjectionConvention_UnableToCreateFieldHandler(this, type)); default: _fieldInterceptors.Add(instance); break; } } foreach ((Type? type, IProjectionOptimizer? instance) in Definition.Optimizers) { switch (instance) { case null when services.TryGetOrCreateService( type, out IProjectionOptimizer?service): _optimizer.Add(service); break; case null: throw new SchemaException( ProjectionConvention_UnableToCreateFieldHandler(this, type)); default: _optimizer.Add(instance); break; } } }
public virtual void ProcessForeignKeyOwnershipChanged( IConventionForeignKeyBuilder relationshipBuilder, IConventionContext <bool?> context) { relationshipBuilder.Metadata.PrincipalToDependent?.Builder.AutoInclude(relationshipBuilder.Metadata.IsOwnership); }
/// <inheritdoc /> public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext <IConventionModelBuilder> context) { var sql = new StringBuilder(); foreach (var entityType in modelBuilder.Metadata.GetEntityTypes()) { foreach (var property in entityType.GetDeclaredProperties()) { var typeMapping = property.FindTypeMapping(); var propertyType = (property.PropertyInfo ?? (MemberInfo)property.FieldInfo)?.GetMemberType(); if ((propertyType?.IsEnum ?? false) && typeMapping != null && !propertyType.IsDefined(typeof(FlagsAttribute), true)) { var enumValues = Enum.GetValues(propertyType); if (enumValues.Length <= 0) { continue; } sql.Clear(); sql.Append("["); sql.Append(property.GetColumnName()); sql.Append("] IN(");; foreach (var item in enumValues) { var value = ((RelationalTypeMapping)typeMapping).GenerateSqlLiteral(item); sql.Append($"{value}, "); } sql.Remove(sql.Length - 2, 2); sql.Append(")"); var constraintName = $"CK_{entityType.GetTableName()}_{property.GetColumnName()}_Enum_Constraint"; entityType.AddCheckConstraint(constraintName, sql.ToString()); } } } }
public override void Merge(IConventionContext context, Convention convention) { if (Definition is {} &&