private void GenerateIndexAttributes(IEntityType entityType) { // Do not generate IndexAttributes for indexes which // would be generated anyway by convention. foreach (var index in entityType.GetIndexes().Where( i => ConfigurationSource.Convention != ((IConventionIndex)i).GetConfigurationSource())) { // If there are annotations that cannot be represented using an IndexAttribute then use fluent API instead. var annotations = _annotationCodeGenerator .FilterIgnoredAnnotations(index.GetAnnotations()) .ToDictionary(a => a.Name, a => a); _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(index, annotations); if (annotations.Count == 0) { var indexAttribute = new AttributeWriter(nameof(IndexAttribute)); foreach (var property in index.Properties) { indexAttribute.AddParameter(_code.Literal(property.Name)); } if (index.Name != null) { indexAttribute.AddParameter($"{nameof(IndexAttribute.Name)} = {_code.Literal(index.Name)}"); } if (index.IsUnique) { indexAttribute.AddParameter($"{nameof(IndexAttribute.IsUnique)} = {_code.Literal(index.IsUnique)}"); } _sb.AppendLine(indexAttribute.ToString()); } } }
private void GenerateIndex(IIndex index) { var annotations = _annotationCodeGenerator .FilterIgnoredAnnotations(index.GetAnnotations()) .ToDictionary(a => a.Name, a => a); _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(index, annotations); var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasIndex)}({_code.Lambda(index.Properties, "e")}, " + $"{_code.Literal(index.GetDatabaseName())})" }; annotations.Remove(RelationalAnnotationNames.Name); if (index.IsUnique) { lines.Add($".{nameof(IndexBuilder.IsUnique)}()"); } lines.AddRange( _annotationCodeGenerator.GenerateFluentApiCalls(index, annotations).Select(m => _code.Fragment(m)) .Concat(GenerateAnnotations(annotations.Values))); AppendMultiLineFluentApi(index.DeclaringEntityType, lines); }
private void GenerateTableAttribute(IEntityType entityType) { var tableName = entityType.GetTableName(); var schema = entityType.GetSchema(); var defaultSchema = entityType.Model.GetDefaultSchema(); var schemaParameterNeeded = schema != null && schema != defaultSchema; var isView = entityType.GetViewName() != null; var tableAttributeNeeded = !isView && (schemaParameterNeeded || tableName != null && tableName != entityType.GetDbSetName()); if (tableAttributeNeeded) { var tableAttribute = new AttributeWriter(nameof(TableAttribute)); tableAttribute.AddParameter(_code.Literal(tableName)); if (schemaParameterNeeded) { tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {_code.Literal(schema)}"); } _sb.AppendLine(tableAttribute.ToString()); } }
private void GenerateKey(IKey key, bool useDataAnnotations) { 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 && annotations.Count == 0) { if (key is Key concreteKey && key.Properties.SequenceEqual( new KeyDiscoveryConvention(null).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)}" + $"({_code.Literal(key.Relational().Name)})"); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(key, annotation)) { annotationsToRemove.Add(annotation); } else { var methodCall = _annotationCodeGenerator.GenerateFluentApi(key, annotation); var line = methodCall == null #pragma warning disable CS0618 // Type or member is obsolete ? _annotationCodeGenerator.GenerateFluentApi(key, annotation, Language) #pragma warning restore CS0618 // Type or member is obsolete : _code.Fragment(methodCall); if (line != null) { lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); AppendMultiLineFluentApi(key.DeclaringEntityType, lines); }
private void GenerateKey(IKey key, IEntityType entityType, bool useDataAnnotations) { if (key == null) { if (!useDataAnnotations) { var line = new List <string> { $".{nameof(EntityTypeBuilder.HasNoKey)}()" }; AppendMultiLineFluentApi(entityType, line); } return; } var annotations = _annotationCodeGenerator .FilterIgnoredAnnotations(key.GetAnnotations()) .ToDictionary(a => a.Name, a => a); _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(key, annotations); var explicitName = key.GetName() != key.GetDefaultName(); annotations.Remove(RelationalAnnotationNames.Name); if (key.Properties.Count == 1 && annotations.Count == 0) { if (key is Key concreteKey && key.Properties.SequenceEqual( KeyDiscoveryConvention.DiscoverKeyProperties( concreteKey.DeclaringEntityType, concreteKey.DeclaringEntityType.GetProperties()))) { return; } if (!explicitName && useDataAnnotations) { return; } } var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasKey)}({_code.Lambda(key.Properties, "e")})" }; if (explicitName) { lines.Add( $".{nameof(RelationalKeyBuilderExtensions.HasName)}({_code.Literal(key.GetName())})"); } lines.AddRange( _annotationCodeGenerator.GenerateFluentApiCalls(key, annotations).Select(m => _code.Fragment(m)) .Concat(GenerateAnnotations(annotations.Values))); AppendMultiLineFluentApi(key.DeclaringEntityType, lines); }
private void GenerateEntityType(IEntityType entityType, bool useDataAnnotations) { GenerateKey(entityType.FindPrimaryKey(), entityType, useDataAnnotations); var annotations = entityType.GetAnnotations().ToList(); RemoveAnnotation(ref annotations, CoreAnnotationNames.ConstructorBinding); RemoveAnnotation(ref annotations, RelationalAnnotationNames.TableName); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Comment); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Schema); RemoveAnnotation(ref annotations, RelationalAnnotationNames.TableMappings); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.DbSetName); RemoveAnnotation(ref annotations, RelationalAnnotationNames.ViewDefinition); var isView = entityType.FindAnnotation(RelationalAnnotationNames.ViewDefinition) != null; if (!useDataAnnotations || isView) { GenerateTableName(entityType); } var annotationsToRemove = new List <IAnnotation>(); var lines = new List <string>(); foreach (var annotation in annotations) { if (annotation.Value == null || _annotationCodeGenerator.IsHandledByConvention(entityType, annotation)) { annotationsToRemove.Add(annotation); } else { var methodCall = _annotationCodeGenerator.GenerateFluentApi(entityType, annotation); if (methodCall != null) { lines.Add(_code.Fragment(methodCall)); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); if (entityType.GetComment() != null) { lines.Add( $".{nameof(RelationalEntityTypeBuilderExtensions.HasComment)}" + $"({_code.Literal(entityType.GetComment())})"); } AppendMultiLineFluentApi(entityType, lines); foreach (var index in entityType.GetIndexes()) { GenerateIndex(index); } foreach (var property in entityType.GetProperties()) { GenerateProperty(property, useDataAnnotations); } foreach (var foreignKey in entityType.GetForeignKeys()) { GenerateRelationship(foreignKey, useDataAnnotations); } }
private void GenerateKey(IKey key, IEntityType entityType, bool useDataAnnotations) { if (key == null) { var line = new List <string> { $".{nameof(EntityTypeBuilder.HasNoKey)}()" }; AppendMultiLineFluentApi(entityType, line); return; } var annotations = key.GetAnnotations().ToList(); var explicitName = key.GetName() != key.GetDefaultName(); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name); if (key.Properties.Count == 1 && annotations.Count == 0) { if (key is Key concreteKey && key.Properties.SequenceEqual( KeyDiscoveryConvention.DiscoverKeyProperties( concreteKey.DeclaringEntityType, concreteKey.DeclaringEntityType.GetProperties()))) { 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)}" + $"({_code.Literal(key.GetName())})"); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (annotation.Value == null || _annotationCodeGenerator.IsHandledByConvention(key, annotation)) { annotationsToRemove.Add(annotation); } else { var methodCall = _annotationCodeGenerator.GenerateFluentApi(key, annotation); if (methodCall != null) { lines.Add(_code.Fragment(methodCall)); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); AppendMultiLineFluentApi(key.DeclaringEntityType, lines); }
private void GenerateMethodSignature(MethodInfo m, IndentedStringBuilder builder, bool displayTypes) { builder.Append(m.Name); if (m.ContainsGenericParameters) { var first = true; builder.Append("<"); foreach (var argType in m.GetGenericArguments().Where(a => a.IsGenericParameter)) { if (first) { first = false; } else { builder.Append(", "); } builder.Append(argType.Name); } builder.Append(">"); } builder.Append("("); var firstParameter = true; foreach (var p in m.GetParameters()) { if (!firstParameter) { builder.Append(", "); } else { firstParameter = false; } if (p.IsOut) { builder.Append("out "); } if (p.ParameterType.IsByRef) { builder.Append("ref "); } if (displayTypes) { builder .Append(_code.Reference(p.ParameterType, true)) .Append(" "); } builder .Append(_code.Identifier(p.Name)); if (p.HasDefaultValue && displayTypes) { if (p.DefaultValue == null) { builder.Append(" = null"); } else { builder .Append(" = ") .Append(_code.Literal((dynamic)p.DefaultValue)); } } } builder.Append(")"); if (m.ContainsGenericParameters && displayTypes) { GenerateGenericParametersConstraints(m, builder); } }