/// <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> protected virtual void GenerateOnModelCreating( [NotNull] IModel model, bool useDataAnnotations) { Check.NotNull(model, nameof(model)); _sb.AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)"); _sb.Append("{"); var annotations = model.GetAnnotations().ToList(); RemoveAnnotation(ref annotations, ChangeDetector.SkipDetectChangesAnnotation); RemoveAnnotation(ref annotations, RelationalAnnotationNames.MaxIdentifierLength); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.DatabaseName); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.EntityTypeErrors); var annotationsToRemove = new List <IAnnotation>(); annotationsToRemove.AddRange( annotations.Where( a => a.Name.StartsWith(RelationalAnnotationNames.SequencePrefix, StringComparison.Ordinal))); var lines = new List <string>(); foreach (var annotation in annotations) { if (annotation.Value == null || _annotationCodeGenerator.IsHandledByConvention(model, annotation)) { annotationsToRemove.Add(annotation); } else { var methodCall = _annotationCodeGenerator.GenerateFluentApi(model, annotation); var line = methodCall == null #pragma warning disable CS0618 // Type or member is obsolete ? _annotationCodeGenerator.GenerateFluentApi(model, 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))); if (lines.Count > 0) { using (_sb.Indent()) { _sb.AppendLine(); _sb.Append("modelBuilder" + lines[0]); using (_sb.Indent()) { foreach (var line in lines.Skip(1)) { _sb.AppendLine(); _sb.Append(line); } } _sb.AppendLine(";"); } } using (_sb.Indent()) { foreach (var entityType in model.GetEntityTypes()) { _entityTypeBuilderInitialized = false; GenerateEntityType(entityType, useDataAnnotations); if (_entityTypeBuilderInitialized) { _sb.AppendLine("});"); } } foreach (var sequence in model.Relational().Sequences) { GenerateSequence(sequence); } } _sb.AppendLine("}"); }
protected virtual void GenerateOnModelCreating( [NotNull] IModel model, bool useDataAnnotations) { Check.NotNull(model, nameof(model)); _sb.AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)"); _sb.Append("{"); var annotations = model.GetAnnotations().ToList(); RemoveAnnotation(ref annotations, CoreAnnotationNames.ProductVersion); RemoveAnnotation(ref annotations, CoreAnnotationNames.ChangeTrackingStrategy); RemoveAnnotation(ref annotations, CoreAnnotationNames.OwnedTypes); RemoveAnnotation(ref annotations, ChangeDetector.SkipDetectChangesAnnotation); RemoveAnnotation(ref annotations, RelationalAnnotationNames.MaxIdentifierLength); RemoveAnnotation(ref annotations, RelationalAnnotationNames.RelationalModel); RemoveAnnotation(ref annotations, RelationalAnnotationNames.CheckConstraints); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Sequences); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.DatabaseName); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.EntityTypeErrors); var annotationsToRemove = new List <IAnnotation>(); var lines = new List <string>(); foreach (var annotation in annotations) { if (annotation.Value == null || _annotationCodeGenerator.IsHandledByConvention(model, annotation)) { annotationsToRemove.Add(annotation); } else { var methodCall = _annotationCodeGenerator.GenerateFluentApi(model, annotation); if (methodCall != null) { lines.Add(_code.Fragment(methodCall)); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); if (lines.Count > 0) { using (_sb.Indent()) { _sb.AppendLine(); _sb.Append("modelBuilder" + lines[0]); using (_sb.Indent()) { foreach (var line in lines.Skip(1)) { _sb.AppendLine(); _sb.Append(line); } } _sb.AppendLine(";"); } } using (_sb.Indent()) { foreach (var entityType in model.GetEntityTypes()) { _entityTypeBuilderInitialized = false; GenerateEntityType(entityType, useDataAnnotations); if (_entityTypeBuilderInitialized) { _sb.AppendLine("});"); } } foreach (var sequence in model.GetSequences()) { GenerateSequence(sequence); } } _sb.AppendLine(); using (_sb.Indent()) { _sb.AppendLine("OnModelCreatingPartial(modelBuilder);"); } _sb.AppendLine("}"); }
/// <summary> /// Generate OnModelBuilding method. /// </summary> /// <param name="model">Metadata about the shape of entities, the relationships between them, and how they map to the database.</param> /// <param name="useDataAnnotations">Use fluent modeling API if false.</param> protected virtual void GenerateOnModelCreating(IModel model, bool useDataAnnotations) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var sb = new IndentedStringBuilder(); using (sb.Indent()) { sb.IncrementIndent(); sb.AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)"); sb.Append("{"); var annotations = model.GetAnnotations().ToList(); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.DatabaseName); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.EntityTypeErrors); var annotationsToRemove = new List <IAnnotation>(); annotationsToRemove.AddRange(annotations.Where(a => a.Name.StartsWith(RelationalAnnotationNames.SequencePrefix, StringComparison.Ordinal))); var lines = new List <string>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(model, annotation)) { annotationsToRemove.Add(annotation); } else { #pragma warning disable CS0618 // Type or member is obsolete var line = _annotationCodeGenerator.GenerateFluentApi(model, annotation, Language); #pragma warning restore CS0618 // Type or member is obsolete if (line != null) { lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); if (lines.Count > 0) { using (sb.Indent()) { sb.AppendLine(); sb.Append("modelBuilder" + lines[0]); using (sb.Indent()) { foreach (var line in lines.Skip(1)) { sb.AppendLine(); sb.Append(line); } } sb.AppendLine(";"); } } using (sb.Indent()) { foreach (var entityType in model.GetEntityTypes()) { _entityTypeBuilderInitialized = false; GenerateEntityType(entityType, useDataAnnotations, sb); if (_entityTypeBuilderInitialized) { sb.AppendLine("});"); } } foreach (var sequence in model.Relational().Sequences) { GenerateSequence(sequence, sb); } } sb.Append("}"); var onModelCreating = sb.ToString(); _templateData.Add("on-model-creating", onModelCreating); } }
private void GenerateOnModelCreating(IModel model, bool useDataAnnotations) { _sb.AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)"); _sb.Append("{"); var annotations = model.GetAnnotations().ToList(); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.DatabaseName); RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.EntityTypeErrors); var annotationsToRemove = new List <IAnnotation>(); annotationsToRemove.AddRange(annotations.Where(a => a.Name.StartsWith(RelationalAnnotationNames.SequencePrefix, StringComparison.Ordinal))); var lines = new List <string>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(model, annotation)) { annotationsToRemove.Add(annotation); } else { var line = _annotationCodeGenerator.GenerateFluentApi(model, annotation, Language); if (line != null) { lines.Add(line); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); if (lines.Count > 0) { using (_sb.Indent()) { _sb.AppendLine(); _sb.Append("modelBuilder" + lines[0]); using (_sb.Indent()) { foreach (var line in lines.Skip(1)) { _sb.AppendLine(); _sb.Append(line); } } _sb.AppendLine(";"); } } using (_sb.Indent()) { foreach (var entityType in model.GetEntityTypes()) { _entityTypeBuilderInitialized = false; GenerateEntityType(entityType, useDataAnnotations); if (_entityTypeBuilderInitialized) { _sb.AppendLine("});"); } } foreach (var sequence in model.Relational().Sequences) { GenerateSequence(sequence); } } _sb.AppendLine("}"); }