Skip to content

Commit

Permalink
Remove SqlServer.Design
Browse files Browse the repository at this point in the history
Remove SqlServer.Design.Tests

Introduce IAnnotationRenderer interface for provider writers to provide us how to interpret annotations
Flow annotations from DatabaseModel to IModel so that provider can generate provider specific fluent API
Remove special processing inside of SqlServerScaffoldingFactory. All processing should happen while generating database
  • Loading branch information
smitpatel committed Jun 2, 2017
1 parent 4e9aa56 commit 3e55cb9
Show file tree
Hide file tree
Showing 69 changed files with 834 additions and 2,179 deletions.
14 changes: 0 additions & 14 deletions EFCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Relational", "src\EF
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Sqlite.Core", "src\EFCore.Sqlite.Core\EFCore.Sqlite.Core.csproj", "{A257C01B-BB91-44BA-831C-1E04F7800AC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.SqlServer.Design", "src\EFCore.SqlServer.Design\EFCore.SqlServer.Design.csproj", "{DA30FC85-8D88-4BB2-98CE-B8A5845BB3EA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Relational.Design", "src\EFCore.Relational.Design\EFCore.Relational.Design.csproj", "{1942C281-C12B-4818-8CC8-C42842871FF5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.SqlServer", "src\EFCore.SqlServer\EFCore.SqlServer.csproj", "{99595B81-D47C-40BA-8C61-5328A5A0E4AB}"
Expand All @@ -39,8 +37,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Sqlite.Tests", "test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.SqlServer.Design.FunctionalTests", "test\EFCore.SqlServer.Design.FunctionalTests\EFCore.SqlServer.Design.FunctionalTests.csproj", "{15033D6B-D415-4932-9462-F2A5AE0B75E8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.SqlServer.Design.Tests", "test\EFCore.SqlServer.Design.Tests\EFCore.SqlServer.Design.Tests.csproj", "{C56644A1-114D-4401-AC79-4B9EC4EDD1E3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.SqlServer.FunctionalTests", "test\EFCore.SqlServer.FunctionalTests\EFCore.SqlServer.FunctionalTests.csproj", "{87AB43B7-767B-467B-9AA9-47BADF850D6A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.SqlServer.Tests", "test\EFCore.SqlServer.Tests\EFCore.SqlServer.Tests.csproj", "{7D1C4E40-0DE6-4C50-AB84-CA8647EA92DF}"
Expand Down Expand Up @@ -103,10 +99,6 @@ Global
{A257C01B-BB91-44BA-831C-1E04F7800AC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A257C01B-BB91-44BA-831C-1E04F7800AC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A257C01B-BB91-44BA-831C-1E04F7800AC8}.Release|Any CPU.Build.0 = Release|Any CPU
{DA30FC85-8D88-4BB2-98CE-B8A5845BB3EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA30FC85-8D88-4BB2-98CE-B8A5845BB3EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA30FC85-8D88-4BB2-98CE-B8A5845BB3EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA30FC85-8D88-4BB2-98CE-B8A5845BB3EA}.Release|Any CPU.Build.0 = Release|Any CPU
{1942C281-C12B-4818-8CC8-C42842871FF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1942C281-C12B-4818-8CC8-C42842871FF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1942C281-C12B-4818-8CC8-C42842871FF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -151,10 +143,6 @@ Global
{15033D6B-D415-4932-9462-F2A5AE0B75E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15033D6B-D415-4932-9462-F2A5AE0B75E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15033D6B-D415-4932-9462-F2A5AE0B75E8}.Release|Any CPU.Build.0 = Release|Any CPU
{C56644A1-114D-4401-AC79-4B9EC4EDD1E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C56644A1-114D-4401-AC79-4B9EC4EDD1E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C56644A1-114D-4401-AC79-4B9EC4EDD1E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C56644A1-114D-4401-AC79-4B9EC4EDD1E3}.Release|Any CPU.Build.0 = Release|Any CPU
{87AB43B7-767B-467B-9AA9-47BADF850D6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87AB43B7-767B-467B-9AA9-47BADF850D6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87AB43B7-767B-467B-9AA9-47BADF850D6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -244,7 +232,6 @@ Global
{6B102CC4-4396-4A7B-9F72-2C6B5C4D8310} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{6A25DF99-2615-46D8-9532-821764647EE1} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{A257C01B-BB91-44BA-831C-1E04F7800AC8} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{DA30FC85-8D88-4BB2-98CE-B8A5845BB3EA} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{1942C281-C12B-4818-8CC8-C42842871FF5} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{99595B81-D47C-40BA-8C61-5328A5A0E4AB} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{7EAC2B8E-4AF6-40D2-95C0-A6662762A7E0} = {258D5057-81B9-40EC-A872-D21E27452749}
Expand All @@ -256,7 +243,6 @@ Global
{7BB7D051-56D7-4A40-A29E-3801F5C19239} = {258D5057-81B9-40EC-A872-D21E27452749}
{7E436D99-82A6-496C-A725-0819CBED056D} = {258D5057-81B9-40EC-A872-D21E27452749}
{15033D6B-D415-4932-9462-F2A5AE0B75E8} = {258D5057-81B9-40EC-A872-D21E27452749}
{C56644A1-114D-4401-AC79-4B9EC4EDD1E3} = {258D5057-81B9-40EC-A872-D21E27452749}
{87AB43B7-767B-467B-9AA9-47BADF850D6A} = {258D5057-81B9-40EC-A872-D21E27452749}
{7D1C4E40-0DE6-4C50-AB84-CA8647EA92DF} = {258D5057-81B9-40EC-A872-D21E27452749}
{313F46FE-9962-4A15-805F-FCBDF5A6181E} = {258D5057-81B9-40EC-A872-D21E27452749}
Expand Down
212 changes: 212 additions & 0 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
Expand All @@ -24,6 +27,7 @@ public class CSharpDbContextGenerator

private readonly CSharpUtilities _cSharpUtilities;
private readonly IScaffoldingHelper _scaffoldingHelper;
private readonly IAnnotationRenderer _annotationRenderer;
private IndentedStringBuilder _sb;
private bool _entityTypeBuilderInitialized;

Expand All @@ -33,12 +37,15 @@ public class CSharpDbContextGenerator
/// </summary>
public CSharpDbContextGenerator(
[NotNull] IScaffoldingHelper scaffoldingHelper,
[NotNull] IAnnotationRenderer annotationRenderer,
[NotNull] CSharpUtilities cSharpUtilities)
{
Check.NotNull(scaffoldingHelper, nameof(scaffoldingHelper));
Check.NotNull(annotationRenderer, nameof(annotationRenderer));
Check.NotNull(cSharpUtilities, nameof(cSharpUtilities));

_scaffoldingHelper = scaffoldingHelper;
_annotationRenderer = annotationRenderer;
_cSharpUtilities = cSharpUtilities;
}

Expand Down Expand Up @@ -147,6 +154,55 @@ 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 (_annotationRenderer.IsHandledByConvention(model, annotation))
{
annotationsToRemove.Add(annotation);
}
else
{
var line = _annotationRenderer.GenerateFluentApi(model, annotation);

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())
Expand Down Expand Up @@ -186,11 +242,41 @@ private void GenerateEntityType(IEntityType entityType, bool useDataAnnotations)
{
GenerateKey(entityType.FindPrimaryKey(), useDataAnnotations);

var annotations = entityType.GetAnnotations().ToList();
RemoveAnnotation(ref annotations, RelationalAnnotationNames.TableName);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.Schema);
RemoveAnnotation(ref annotations, ScaffoldingAnnotationNames.DbSetName);

if (!useDataAnnotations)
{
GenerateTableName(entityType);
}

var annotationsToRemove = new List<IAnnotation>();
var lines = new List<string>();

foreach (var annotation in annotations)
{
if (_annotationRenderer.IsHandledByConvention(entityType, annotation))
{
annotationsToRemove.Add(annotation);
}
else
{
var line = _annotationRenderer.GenerateFluentApi(entityType, annotation);

if (line != null)
{
lines.Add(line);
annotationsToRemove.Add(annotation);
}
}
}

lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove)));

AppendMultiLineFluentApi(entityType, lines);

foreach (var index in entityType.GetIndexes())
{
GenerateIndex(index);
Expand Down Expand Up @@ -242,7 +328,10 @@ private void GenerateKey(IKey key, bool useDataAnnotations)
return;
}

var annotations = key.GetAnnotations().ToList();

var explicitName = key.Relational().Name != new RelationalKeyAnnotations(key).GetDefaultName();
RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name);

if (key.Properties.Count == 1)
{
Expand Down Expand Up @@ -271,6 +360,28 @@ private void GenerateKey(IKey key, bool useDataAnnotations)
lines.Add($".{nameof(RelationalKeyBuilderExtensions.HasName)}({_cSharpUtilities.DelimitString(key.Relational().Name)})");
}

var annotationsToRemove = new List<IAnnotation>();

foreach (var annotation in annotations)
{
if (_annotationRenderer.IsHandledByConvention(key, annotation))
{
annotationsToRemove.Add(annotation);
}
else
{
var line = _annotationRenderer.GenerateFluentApi(key, annotation);

if (line != null)
{
lines.Add(line);
annotationsToRemove.Add(annotation);
}
}
}

lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove)));

AppendMultiLineFluentApi(key.DeclaringEntityType, lines);
}

Expand Down Expand Up @@ -307,9 +418,12 @@ private void GenerateIndex(IIndex index)
$".{nameof(EntityTypeBuilder.HasIndex)}(e => {GenerateLambdaToKey(index.Properties, "e")})"
};

var annotations = index.GetAnnotations().ToList();

if (!string.IsNullOrEmpty(index.Relational().Name))
{
lines.Add($".{nameof(RelationalIndexBuilderExtensions.HasName)}({_cSharpUtilities.DelimitString(index.Relational().Name)})");
RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name);
}

if (index.IsUnique)
Expand All @@ -320,8 +434,31 @@ private void GenerateIndex(IIndex index)
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 (_annotationRenderer.IsHandledByConvention(index, annotation))
{
annotationsToRemove.Add(annotation);
}
else
{
var line = _annotationRenderer.GenerateFluentApi(index, annotation);

if (line != null)
{
lines.Add(line);
annotationsToRemove.Add(annotation);
}
}
}

lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove)));

AppendMultiLineFluentApi(index.DeclaringEntityType, lines);
}

Expand All @@ -332,6 +469,17 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
$".{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
Expand Down Expand Up @@ -411,6 +559,28 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
lines.Add($".{methodName}()");
}

var annotationsToRemove = new List<IAnnotation>();

foreach (var annotation in annotations)
{
if (_annotationRenderer.IsHandledByConvention(property, annotation))
{
annotationsToRemove.Add(annotation);
}
else
{
var line = _annotationRenderer.GenerateFluentApi(property, annotation);

if (line != null)
{
lines.Add(line);
annotationsToRemove.Add(annotation);
}
}
}

lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove)));

switch (lines.Count)
{
case 1:
Expand All @@ -429,6 +599,8 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotations)
{
var canUseDataAnnotations = true;
var annotations = foreignKey.GetAnnotations().ToList();

var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.HasOne)}(d => d.{foreignKey.DependentToPrincipal.Name})",
Expand Down Expand Up @@ -458,6 +630,8 @@ private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotation
lines.Add($".{nameof(ReferenceReferenceBuilder.OnDelete)}({_cSharpUtilities.GenerateLiteral(foreignKey.DeleteBehavior)})");
}

RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name);

if (foreignKey.Relational().Name !=
RelationalForeignKeyAnnotations.GetDefaultForeignKeyName(
foreignKey.DeclaringEntityType.Relational().TableName,
Expand All @@ -468,6 +642,29 @@ private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotation
lines.Add($".{nameof(RelationalReferenceReferenceBuilderExtensions.HasConstraintName)}({_cSharpUtilities.DelimitString(foreignKey.Relational().Name)})");
}

var annotationsToRemove = new List<IAnnotation>();

foreach (var annotation in annotations)
{
if (_annotationRenderer.IsHandledByConvention(foreignKey, annotation))
{
annotationsToRemove.Add(annotation);
}
else
{
var line = _annotationRenderer.GenerateFluentApi(foreignKey, annotation);

if (line != null)
{
canUseDataAnnotations = false;
lines.Add(line);
annotationsToRemove.Add(annotation);
}
}
}

lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove)));

if (!useDataAnnotations || !canUseDataAnnotations)
{
AppendMultiLineFluentApi(foreignKey.DeclaringEntityType, lines);
Expand Down Expand Up @@ -557,5 +754,20 @@ private string GenerateLambdaToKey(
? $"{lambdaIdentifier}.{properties[0].Name}"
: $"new {{ {string.Join(", ", properties.Select(p => lambdaIdentifier + "." + p.Name))} }}";
}

private void RemoveAnnotation(ref List<IAnnotation> annotations, string annotationName)
{
annotations.Remove(annotations.SingleOrDefault(a => a.Name == annotationName));
}

private IList<string> GenerateAnnotations(IEnumerable<IAnnotation> annotations)
{
return annotations.Select(GenerateAnnotation).ToList();
}

private string GenerateAnnotation(IAnnotation annotation)
{
return $".HasAnnotation({_cSharpUtilities.DelimitString(annotation.Name)}, {_cSharpUtilities.GenerateLiteral((dynamic)annotation.Value)})";
}
}
}
Loading

0 comments on commit 3e55cb9

Please sign in to comment.