public virtual void AddColumnNameAndTypeConfiguration( [NotNull] PropertyConfiguration propertyConfiguration) { Check.NotNull(propertyConfiguration, nameof(propertyConfiguration)); var delimitedColumnName = ExtensionsProvider.For(propertyConfiguration.Property).ColumnName != null && ExtensionsProvider.For(propertyConfiguration.Property).ColumnName != propertyConfiguration.Property.Name ? CSharpUtilities.DelimitString( ExtensionsProvider.For(propertyConfiguration.Property).ColumnName) : null; var delimitedColumnTypeName = ExtensionsProvider.For(propertyConfiguration.Property).ColumnType != null ? CSharpUtilities.DelimitString( ExtensionsProvider.For(propertyConfiguration.Property).ColumnType) : null; if (delimitedColumnName != null) { propertyConfiguration.FluentApiConfigurations.Add( new FluentApiConfiguration( nameof(RelationalPropertyBuilderExtensions.HasColumnName), delimitedColumnName) { HasAttributeEquivalent = true }); if (delimitedColumnTypeName == null) { propertyConfiguration.AttributeConfigurations.Add( new AttributeConfiguration(nameof(ColumnAttribute), delimitedColumnName)); } else { propertyConfiguration.FluentApiConfigurations.Add( new FluentApiConfiguration( nameof(RelationalPropertyBuilderExtensions.HasColumnType), delimitedColumnTypeName) { HasAttributeEquivalent = true }); propertyConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(ColumnAttribute), new[] { delimitedColumnName, nameof(ColumnAttribute.TypeName) + " = " + delimitedColumnTypeName })); } } else if (delimitedColumnTypeName != null) { propertyConfiguration.FluentApiConfigurations.Add( new FluentApiConfiguration( nameof(RelationalPropertyBuilderExtensions.HasColumnType), delimitedColumnTypeName)); } }
private void GenerateColumnAttribute(IProperty property) { var columnName = property.Relational().ColumnName; var columnType = property.GetConfiguredColumnType(); var delimitedColumnName = columnName != null && columnName != property.Name ? CSharpUtilities.DelimitString(columnName) : null; var delimitedColumnType = columnType != null?CSharpUtilities.DelimitString(columnType) : null; if ((delimitedColumnName ?? delimitedColumnType) != null) { var columnAttribute = new AttributeWriter(nameof(ColumnAttribute)); if (delimitedColumnName != null) { columnAttribute.AddParameter(delimitedColumnName); } if (delimitedColumnType != null) { columnAttribute.AddParameter($"{nameof(ColumnAttribute.TypeName)} = {delimitedColumnType}"); } _propertyAnnotations.Add(new Dictionary <string, object> { { "property-annotation", columnAttribute }, }); } }
private void GenerateTableName(IEntityType entityType, IndentedStringBuilder sb) { var tableName = entityType.Relational().TableName; var schema = entityType.Relational().Schema; var defaultSchema = entityType.Model.Relational().DefaultSchema; var explicitSchema = schema != null && schema != defaultSchema; var explicitTable = explicitSchema || tableName != null && tableName != entityType.Scaffolding().DbSetName; if (explicitTable) { var parameterString = CSharpUtilities.DelimitString(tableName); if (explicitSchema) { parameterString += ", " + CSharpUtilities.DelimitString(schema); } var lines = new List <string> { $".{nameof(RelationalEntityTypeBuilderExtensions.ToTable)}({parameterString})" }; AppendMultiLineFluentApi(entityType, lines, sb); } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void AddColumnNameAndTypeConfiguration( [NotNull] PropertyConfiguration propertyConfiguration) { Check.NotNull(propertyConfiguration, nameof(propertyConfiguration)); var delimitedColumnName = AnnotationProvider.For(propertyConfiguration.Property).ColumnName != null && AnnotationProvider.For(propertyConfiguration.Property).ColumnName != propertyConfiguration.Property.Name ? CSharpUtilities.DelimitString( AnnotationProvider.For(propertyConfiguration.Property).ColumnName) : null; var delimitedColumnTypeName = AnnotationProvider.For(propertyConfiguration.Property).ColumnType != null ? CSharpUtilities.DelimitString( AnnotationProvider.For(propertyConfiguration.Property).ColumnType) : null; if (delimitedColumnName != null && delimitedColumnTypeName != null) { propertyConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ true, nameof(RelationalPropertyBuilderExtensions.HasColumnName), delimitedColumnName)); propertyConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ true, nameof(RelationalPropertyBuilderExtensions.HasColumnType), delimitedColumnTypeName)); propertyConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(ColumnAttribute), delimitedColumnName, nameof(ColumnAttribute.TypeName) + " = " + delimitedColumnTypeName)); } else if (delimitedColumnName != null) { propertyConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ true, nameof(RelationalPropertyBuilderExtensions.HasColumnName), delimitedColumnName)); propertyConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration(nameof(ColumnAttribute), delimitedColumnName)); } else if (delimitedColumnTypeName != null) { propertyConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ true, nameof(RelationalPropertyBuilderExtensions.HasColumnType), delimitedColumnTypeName)); propertyConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(ColumnAttribute), nameof(ColumnAttribute.TypeName) + " = " + delimitedColumnTypeName)); } }
private void GenerateIndex(IIndex index, IndentedStringBuilder sb) { var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasIndex)}(e => {GenerateLambdaToKey(index.Properties, "e")})" }; var annotations = index.GetAnnotations().ToList(); if (!string.IsNullOrEmpty((string)index[RelationalAnnotationNames.Name])) { lines.Add($".{nameof(RelationalIndexBuilderExtensions.HasName)}({CSharpUtilities.DelimitString(index.Relational().Name)})"); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name); } if (index.IsUnique) { lines.Add($".{nameof(IndexBuilder.IsUnique)}()"); } if (index.Relational().Filter != null) { lines.Add($".{nameof(RelationalIndexBuilderExtensions.HasFilter)}({CSharpUtilities.DelimitString(index.Relational().Filter)})"); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Filter); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(index, annotation)) { annotationsToRemove.Add(annotation); } else { var line = _annotationCodeGenerator.GenerateFluentApi(index, annotation, Language); if (line != null) { lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); AppendMultiLineFluentApi(index.DeclaringEntityType, lines, sb); }
public virtual void AddDefaultExpressionConfiguration( [NotNull] PropertyConfiguration propertyConfiguration) { Check.NotNull(propertyConfiguration, nameof(propertyConfiguration)); if (ExtensionsProvider.For(propertyConfiguration.Property).GeneratedValueSql != null) { propertyConfiguration.FluentApiConfigurations.Add( new FluentApiConfiguration( nameof(RelationalPropertyBuilderExtensions.HasDefaultValueSql), CSharpUtilities.DelimitString( ExtensionsProvider.For(propertyConfiguration.Property).GeneratedValueSql))); } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void AddComputedExpressionConfiguration( [NotNull] PropertyConfiguration propertyConfiguration) { Check.NotNull(propertyConfiguration, nameof(propertyConfiguration)); if (AnnotationProvider.For(propertyConfiguration.Property).ComputedColumnSql != null) { propertyConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ false, nameof(RelationalPropertyBuilderExtensions.HasComputedColumnSql), CSharpUtilities.DelimitString( AnnotationProvider.For(propertyConfiguration.Property).ComputedColumnSql))); } }
public virtual void AddTableNameConfiguration([NotNull] EntityConfiguration entityConfiguration) { Check.NotNull(entityConfiguration, nameof(entityConfiguration)); var entityType = entityConfiguration.EntityType; if (ExtensionsProvider.For(entityType).Schema != null && ExtensionsProvider.For(entityType).Schema != DefaultSchemaName) { var delimitedTableName = CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).TableName); var delimitedSchemaName = CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).Schema); entityConfiguration.FluentApiConfigurations.Add( new FluentApiConfiguration( nameof(RelationalEntityTypeBuilderExtensions.ToTable), delimitedTableName, delimitedSchemaName) { HasAttributeEquivalent = true }); entityConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(TableAttribute), delimitedTableName, nameof(TableAttribute.Schema) + " = " + delimitedSchemaName)); } else if (ExtensionsProvider.For(entityType).TableName != null && ExtensionsProvider.For(entityType).TableName != entityType.DisplayName()) { var delimitedTableName = CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).TableName); entityConfiguration.FluentApiConfigurations.Add( new FluentApiConfiguration( nameof(RelationalEntityTypeBuilderExtensions.ToTable), delimitedTableName) { HasAttributeEquivalent = true }); entityConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(TableAttribute), delimitedTableName)); } }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.FindInverse(); if (inverseNavigation != null) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); inversePropertyAttribute.AddParameter(CSharpUtilities.DelimitString(inverseNavigation.Name)); _navPropertyAnnotations.Add(new Dictionary <string, object> { { "nav-property-annotation", inversePropertyAttribute.ToString() }, }); } } }
private void GenerateForeignKeyAttribute(INavigation navigation) { if (navigation.IsDependentToPrincipal()) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); foreignKeyAttribute.AddParameter( CSharpUtilities.DelimitString( string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); _navPropertyAnnotations.Add(new Dictionary <string, object> { { "nav-property-annotation", foreignKeyAttribute.ToString() }, }); } } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void AddTableNameConfiguration([NotNull] EntityConfiguration entityConfiguration) { Check.NotNull(entityConfiguration, nameof(entityConfiguration)); var entityType = entityConfiguration.EntityType; if (AnnotationProvider.For(entityType).Schema != null && AnnotationProvider.For(entityType).Schema != AnnotationProvider.For(Model).DefaultSchema) { var delimitedTableName = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).TableName); var delimitedSchemaName = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).Schema); entityConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ true, nameof(RelationalEntityTypeBuilderExtensions.ToTable), delimitedTableName, delimitedSchemaName)); entityConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(TableAttribute), delimitedTableName, nameof(TableAttribute.Schema) + " = " + delimitedSchemaName)); } else if (AnnotationProvider.For(entityType).TableName != null && AnnotationProvider.For(entityType).TableName != entityType.Scaffolding().DbSetName) { var delimitedTableName = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).TableName); entityConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( /* hasAttributeEquivalent */ true, nameof(RelationalEntityTypeBuilderExtensions.ToTable), delimitedTableName)); entityConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(TableAttribute), delimitedTableName)); } }
public override void AddTableNameConfiguration(EntityConfiguration entityConfiguration) { // Rather than being smart, we're just always configuring the // table name for every entity. var entityType = entityConfiguration.EntityType; var delimitedTableName = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).TableName); var delimitedSchemaName = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).Schema); entityConfiguration.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( true, /* <= hasAttributeEquivalent */ nameof(RelationalEntityTypeBuilderExtensions.ToTable), delimitedTableName, delimitedSchemaName)); entityConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(TableAttribute), delimitedTableName, $"{nameof(TableAttribute.Schema)} = {delimitedSchemaName}")); }
private void GenerateTableAttribute(IEntityType entityType) { var tableName = entityType.Relational().TableName; var schema = entityType.Relational().Schema; var defaultSchema = entityType.Model.Relational().DefaultSchema; var schemaParameterNeeded = schema != null && schema != defaultSchema; var tableAttributeNeeded = schemaParameterNeeded || tableName != null && tableName != entityType.Scaffolding().DbSetName; if (tableAttributeNeeded) { var tableAttribute = new AttributeWriter(nameof(TableAttribute)); tableAttribute.AddParameter(CSharpUtilities.DelimitString(tableName)); if (schemaParameterNeeded) { tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {CSharpUtilities.DelimitString(schema)}"); } _templateData.Add("class-annotation", tableAttribute.ToString()); } }
private string GenerateAnnotation(IAnnotation annotation) { return($".HasAnnotation({CSharpUtilities.DelimitString(annotation.Name)}, {CSharpUtilities.GenerateLiteral((dynamic)annotation.Value)})"); }
private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotations, IndentedStringBuilder sb) { var canUseDataAnnotations = true; var annotations = foreignKey.GetAnnotations().ToList(); var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasOne)}(d => d.{foreignKey.DependentToPrincipal.Name})", $".{(foreignKey.IsUnique ? nameof(ReferenceNavigationBuilder.WithOne) : nameof(ReferenceNavigationBuilder.WithMany))}" + $"(p => p.{foreignKey.PrincipalToDependent.Name})" }; if (!foreignKey.PrincipalKey.IsPrimaryKey()) { canUseDataAnnotations = false; lines.Add( $".{nameof(ReferenceReferenceBuilder.HasPrincipalKey)}" + $"{(foreignKey.IsUnique ? $"<{foreignKey.PrincipalEntityType.DisplayName()}>" : "")}" + $"(p => {GenerateLambdaToKey(foreignKey.PrincipalKey.Properties, "p")})"); } lines.Add( $".{nameof(ReferenceReferenceBuilder.HasForeignKey)}" + $"{(foreignKey.IsUnique ? $"<{foreignKey.DeclaringEntityType.DisplayName()}>" : "")}" + $"(d => {GenerateLambdaToKey(foreignKey.Properties, "d")})"); var defaultOnDeleteAction = foreignKey.IsRequired ? DeleteBehavior.Cascade : DeleteBehavior.ClientSetNull; if (foreignKey.DeleteBehavior != defaultOnDeleteAction) { canUseDataAnnotations = false; lines.Add($".{nameof(ReferenceReferenceBuilder.OnDelete)}({CSharpUtilities.GenerateLiteral(foreignKey.DeleteBehavior)})"); } if (!string.IsNullOrEmpty((string)foreignKey[RelationalAnnotationNames.Name])) { canUseDataAnnotations = false; lines.Add($".{nameof(RelationalReferenceReferenceBuilderExtensions.HasConstraintName)}({CSharpUtilities.DelimitString(foreignKey.Relational().Name)})"); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(foreignKey, annotation)) { annotationsToRemove.Add(annotation); } else { var line = _annotationCodeGenerator.GenerateFluentApi(foreignKey, annotation, Language); if (line != null) { canUseDataAnnotations = false; lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); if (!useDataAnnotations || !canUseDataAnnotations) { AppendMultiLineFluentApi(foreignKey.DeclaringEntityType, lines, sb); } }
private void GenerateSequence(ISequence sequence, IndentedStringBuilder sb) { var methodName = nameof(RelationalModelBuilderExtensions.HasSequence); if (sequence.ClrType != Sequence.DefaultClrType) { methodName += $"<{CSharpUtilities.GetTypeName(sequence.ClrType)}>"; } var parameters = CSharpUtilities.DelimitString(sequence.Name); if (string.IsNullOrEmpty(sequence.Schema) && sequence.Model.Relational().DefaultSchema != sequence.Schema) { parameters += $", {CSharpUtilities.DelimitString(sequence.Schema)}"; } var lines = new List <string> { $"modelBuilder.{methodName}({parameters})" }; if (sequence.StartValue != Sequence.DefaultStartValue) { lines.Add($".{nameof(SequenceBuilder.StartsAt)}({sequence.StartValue})"); } if (sequence.IncrementBy != Sequence.DefaultIncrementBy) { lines.Add($".{nameof(SequenceBuilder.IncrementsBy)}({sequence.IncrementBy})"); } if (sequence.MinValue != Sequence.DefaultMinValue) { lines.Add($".{nameof(SequenceBuilder.HasMin)}({sequence.MinValue})"); } if (sequence.MaxValue != Sequence.DefaultMaxValue) { lines.Add($".{nameof(SequenceBuilder.HasMax)}({sequence.MaxValue})"); } if (sequence.IsCyclic != Sequence.DefaultIsCyclic) { lines.Add($".{nameof(SequenceBuilder.IsCyclic)}()"); } if (lines.Count == 2) { lines = new List <string> { lines[0] + lines[1] }; } sb.AppendLine(); sb.Append(lines[0]); using (sb.Indent()) { foreach (var line in lines.Skip(1)) { sb.AppendLine(); sb.Append(line); } } sb.AppendLine(";"); }
private void GenerateProperty(IProperty property, bool useDataAnnotations, IndentedStringBuilder sb) { var lines = new List <string> { $".{nameof(EntityTypeBuilder.Property)}(e => e.{property.Name})" }; var annotations = property.GetAnnotations().ToList(); RemoveAnnotation(ref annotations, RelationalAnnotationNames.ColumnName); RemoveAnnotation(ref annotations, RelationalAnnotationNames.ColumnType); RemoveAnnotation(ref annotations, CoreAnnotationNames.MaxLengthAnnotation); RemoveAnnotation(ref annotations, CoreAnnotationNames.UnicodeAnnotation); RemoveAnnotation(ref annotations, RelationalAnnotationNames.DefaultValue); RemoveAnnotation(ref annotations, RelationalAnnotationNames.DefaultValueSql); RemoveAnnotation(ref annotations, RelationalAnnotationNames.ComputedColumnSql); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.ColumnOrdinal); if (!useDataAnnotations) { if (!property.IsNullable && property.ClrType.IsNullableType() && !property.IsPrimaryKey()) { lines.Add($".{nameof(PropertyBuilder.IsRequired)}()"); } var columnName = property.Relational().ColumnName; if (columnName != null && columnName != property.Name) { lines.Add($".{nameof(RelationalPropertyBuilderExtensions.HasColumnName)}({CSharpUtilities.DelimitString(columnName)})"); } var columnType = property.GetConfiguredColumnType(); if (columnType != null) { lines.Add($".{nameof(RelationalPropertyBuilderExtensions.HasColumnType)}({CSharpUtilities.DelimitString(columnType)})"); } var maxLength = property.GetMaxLength(); if (maxLength.HasValue) { lines.Add($".{nameof(PropertyBuilder.HasMaxLength)}({CSharpUtilities.GenerateLiteral(maxLength.Value)})"); } } if (property.IsUnicode() != null) { lines.Add($".{nameof(PropertyBuilder.IsUnicode)}({(property.IsUnicode() == false ? CSharpUtilities.GenerateLiteral(false) : "")})"); } if (property.Relational().DefaultValue != null) { lines.Add($".{nameof(RelationalPropertyBuilderExtensions.HasDefaultValue)}({CSharpUtilities.GenerateLiteral((dynamic)property.Relational().DefaultValue)})"); } if (property.Relational().DefaultValueSql != null) { lines.Add($".{nameof(RelationalPropertyBuilderExtensions.HasDefaultValueSql)}({CSharpUtilities.DelimitString(property.Relational().DefaultValueSql)})"); } if (property.Relational().ComputedColumnSql != null) { lines.Add($".{nameof(RelationalPropertyBuilderExtensions.HasComputedColumnSql)}({CSharpUtilities.DelimitString(property.Relational().ComputedColumnSql)})"); } var valueGenerated = property.ValueGenerated; var isRowVersion = false; if (((Property)property).GetValueGeneratedConfigurationSource().HasValue && new RelationalValueGeneratorConvention().GetValueGenerated((Property)property) != valueGenerated) { string methodName; switch (valueGenerated) { case ValueGenerated.OnAdd: methodName = nameof(PropertyBuilder.ValueGeneratedOnAdd); break; case ValueGenerated.OnAddOrUpdate: isRowVersion = property.IsConcurrencyToken; methodName = isRowVersion ? nameof(PropertyBuilder.IsRowVersion) : nameof(PropertyBuilder.ValueGeneratedOnAddOrUpdate); break; case ValueGenerated.Never: methodName = nameof(PropertyBuilder.ValueGeneratedNever); break; default: methodName = ""; break; } lines.Add($".{methodName}()"); } if (property.IsConcurrencyToken && !isRowVersion) { lines.Add($".{nameof(PropertyBuilder.IsConcurrencyToken)}()"); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(property, annotation)) { annotationsToRemove.Add(annotation); } else { var line = _annotationCodeGenerator.GenerateFluentApi(property, annotation, Language); if (line != null) { lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); switch (lines.Count) { case 1: return; case 2: lines = new List <string> { lines[0] + lines[1] }; break; } AppendMultiLineFluentApi(property.DeclaringEntityType, lines, sb); }
public virtual void AddNavigationProperties([NotNull] EntityConfiguration entityConfiguration) { Check.NotNull(entityConfiguration, nameof(entityConfiguration)); foreach (var otherEntityType in entityConfiguration.EntityType .Model.EntityTypes.Where(et => et != entityConfiguration.EntityType)) { // set up the navigation properties for foreign keys from another EntityType // which reference this EntityType (i.e. this EntityType is the principal) foreach (var foreignKey in otherEntityType .GetForeignKeys().Where(fk => fk.PrincipalEntityType == entityConfiguration.EntityType)) { if (((EntityType)otherEntityType) .FindAnnotation(RelationalMetadataModelProvider.AnnotationNameEntityTypeError) != null) { entityConfiguration.NavigationPropertyConfigurations.Add( new NavigationPropertyConfiguration( RelationalDesignStrings.UnableToAddNavigationProperty(otherEntityType.Name))); } else { var referencedType = foreignKey.IsUnique ? otherEntityType.Name : "ICollection<" + otherEntityType.Name + ">"; var navPropConfiguration = new NavigationPropertyConfiguration( referencedType, (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName]); if (foreignKey.PrincipalKey.IsPrimaryKey()) { navPropConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(InversePropertyAttribute), CSharpUtilities.DelimitString( (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName]))); } entityConfiguration.NavigationPropertyConfigurations.Add( navPropConfiguration); } } } foreach (var foreignKey in entityConfiguration.EntityType.GetForeignKeys()) { // set up the navigation property on this end of foreign keys owned by this EntityType // (i.e. this EntityType is the dependent) var dependentEndNavPropConfiguration = new NavigationPropertyConfiguration( foreignKey.PrincipalEntityType.Name, (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName]); if (foreignKey.PrincipalKey.IsPrimaryKey()) { dependentEndNavPropConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(ForeignKeyAttribute), CSharpUtilities.DelimitString( string.Join(",", foreignKey.Properties.Select(p => p.Name))))); dependentEndNavPropConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(InversePropertyAttribute), CSharpUtilities.DelimitString( (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName]))); } entityConfiguration.NavigationPropertyConfigurations.Add( dependentEndNavPropConfiguration); // set up the other navigation property for self-referencing foreign keys owned by this EntityType if (((ForeignKey)foreignKey).IsSelfReferencing()) { var referencedType = foreignKey.IsUnique ? foreignKey.DeclaringEntityType.Name : "ICollection<" + foreignKey.DeclaringEntityType.Name + ">"; var principalEndNavPropConfiguration = new NavigationPropertyConfiguration( referencedType, (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName]); principalEndNavPropConfiguration.AttributeConfigurations.Add( new AttributeConfiguration( nameof(InversePropertyAttribute), CSharpUtilities.DelimitString( (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName]))); entityConfiguration.NavigationPropertyConfigurations.Add( principalEndNavPropConfiguration); } } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void AddSequenceConfigurations() { _sequenceConfigurations = new List <SequenceConfiguration>(); foreach (var sequence in AnnotationProvider.For(Model).Sequences) { var config = _configurationFactory.CreateSequenceConfiguration(); config.NameIdentifier = CSharpUtilities.DelimitString(sequence.Name); if (sequence.ClrType != Sequence.DefaultClrType) { config.TypeIdentifier = CSharpUtilities.GetTypeName(sequence.ClrType); } if (!string.IsNullOrEmpty(sequence.Schema) && AnnotationProvider.For(Model).DefaultSchema != sequence.Schema) { config.SchemaNameIdentifier = CSharpUtilities.DelimitString(sequence.Schema); } if (sequence.StartValue != Sequence.DefaultStartValue) { config.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( false, nameof(RelationalSequenceBuilder.StartsAt), sequence.StartValue.ToString(CultureInfo.InvariantCulture))); } if (sequence.IncrementBy != Sequence.DefaultIncrementBy) { config.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( false, nameof(RelationalSequenceBuilder.IncrementsBy), sequence.IncrementBy.ToString(CultureInfo.InvariantCulture))); } if (sequence.MinValue != Sequence.DefaultMinValue) { config.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( false, nameof(RelationalSequenceBuilder.HasMin), sequence.MinValue?.ToString(CultureInfo.InvariantCulture) ?? "")); } if (sequence.MaxValue != Sequence.DefaultMaxValue) { config.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( false, nameof(RelationalSequenceBuilder.HasMax), sequence.MaxValue?.ToString(CultureInfo.InvariantCulture) ?? "")); } if (sequence.IsCyclic != Sequence.DefaultIsCyclic) { config.FluentApiConfigurations.Add( _configurationFactory.CreateFluentApiConfiguration( false, nameof(RelationalSequenceBuilder.IsCyclic))); } _sequenceConfigurations.Add(config); } }
private void GenerateKey(IKey key, bool useDataAnnotations, IndentedStringBuilder sb) { if (key == null) { return; } var annotations = key.GetAnnotations().ToList(); var explicitName = key.Relational().Name != ConstraintNamer.GetDefaultName(key); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name); if (key.Properties.Count == 1) { if (key is Key concreteKey && key.Properties.SequenceEqual(new KeyDiscoveryConvention().DiscoverKeyProperties(concreteKey.DeclaringEntityType, concreteKey.DeclaringEntityType.GetProperties().ToList()))) { return; } if (!explicitName && useDataAnnotations) { return; } } var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasKey)}(e => {GenerateLambdaToKey(key.Properties, "e")})" }; if (explicitName) { lines.Add($".{nameof(RelationalKeyBuilderExtensions.HasName)}({CSharpUtilities.DelimitString(key.Relational().Name)})"); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(key, annotation)) { annotationsToRemove.Add(annotation); } else { var line = _annotationCodeGenerator.GenerateFluentApi(key, annotation, Language); if (line != null) { lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); AppendMultiLineFluentApi(key.DeclaringEntityType, lines, sb); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void AddNavigationProperties([NotNull] EntityConfiguration entityConfiguration) { Check.NotNull(entityConfiguration, nameof(entityConfiguration)); foreach (var otherEntityType in entityConfiguration.EntityType .Model.GetEntityTypes().Where(et => et != entityConfiguration.EntityType)) { // set up the navigation properties for foreign keys from another EntityType // which reference this EntityType (i.e. this EntityType is the principal) foreach (var foreignKey in otherEntityType .GetForeignKeys().Where(fk => fk.PrincipalEntityType == entityConfiguration.EntityType)) { var principalNavProp = foreignKey.PrincipalToDependent; if (principalNavProp != null) { var referencedType = foreignKey.IsUnique ? otherEntityType.Name : "ICollection<" + otherEntityType.Name + ">"; var navPropConfiguration = _configurationFactory.CreateNavigationPropertyConfiguration( referencedType, principalNavProp.Name); var dependentNavProp = foreignKey.DependentToPrincipal; if (foreignKey.PrincipalKey.IsPrimaryKey() && dependentNavProp != null) { navPropConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(InversePropertyAttribute), CSharpUtilities.DelimitString(dependentNavProp.Name))); } entityConfiguration.NavigationPropertyConfigurations.Add(navPropConfiguration); } } } foreach (var foreignKey in entityConfiguration.EntityType.GetForeignKeys()) { // set up the navigation property on this end of foreign keys owned by this EntityType // (i.e. this EntityType is the dependent) var dependentNavProp = foreignKey.DependentToPrincipal; if (dependentNavProp != null) { var dependentEndNavPropConfiguration = _configurationFactory.CreateNavigationPropertyConfiguration( foreignKey.PrincipalEntityType.Name, dependentNavProp.Name); var principalNavProp = foreignKey.PrincipalToDependent; if (foreignKey.PrincipalKey.IsPrimaryKey() && principalNavProp != null) { dependentEndNavPropConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(ForeignKeyAttribute), CSharpUtilities.DelimitString( string.Join(",", foreignKey.Properties.Select(p => p.Name))))); dependentEndNavPropConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(InversePropertyAttribute), CSharpUtilities.DelimitString(principalNavProp.Name))); } entityConfiguration.NavigationPropertyConfigurations.Add( dependentEndNavPropConfiguration); // set up the other navigation property for self-referencing foreign keys owned by this EntityType if (((ForeignKey)foreignKey).IsSelfReferencing() && principalNavProp != null) { var referencedType = foreignKey.IsUnique ? foreignKey.DeclaringEntityType.Name : "ICollection<" + foreignKey.DeclaringEntityType.Name + ">"; var principalEndNavPropConfiguration = _configurationFactory.CreateNavigationPropertyConfiguration( referencedType, principalNavProp.Name); principalEndNavPropConfiguration.AttributeConfigurations.Add( _configurationFactory.CreateAttributeConfiguration( nameof(InversePropertyAttribute), CSharpUtilities.DelimitString(dependentNavProp.Name))); entityConfiguration.NavigationPropertyConfigurations.Add( principalEndNavPropConfiguration); } } } }