From 0121136501568d9d84b585d64b28f93584790b36 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Tue, 15 Sep 2020 14:22:33 -0700 Subject: [PATCH] RevEng: Remove ColumnType annotation from scaffolded code Since we put fluent API --- .../Internal/CSharpDbContextGenerator.cs | 3 +- .../Internal/CSharpDbContextGeneratorTest.cs | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs index 99973653785..d66e3a818fe 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs @@ -582,15 +582,14 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations) } var columnType = property.GetConfiguredColumnType(); - if (columnType != null) { lines.Add( $".{nameof(RelationalPropertyBuilderExtensions.HasColumnType)}({_code.Literal(columnType)})"); + annotations.Remove(RelationalAnnotationNames.ColumnType); } var maxLength = property.GetMaxLength(); - if (maxLength.HasValue) { lines.Add( diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs index d30134dd231..037f10cbffb 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs @@ -684,6 +684,78 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) model => { }); } + [ConditionalFact] + public void Column_type_is_not_scaffolded_as_annotation() + { + Test( + modelBuilder => modelBuilder + .Entity( + "Employee", + x => + { + x.Property("Id"); + x.Property("HireDate").HasColumnType("date").HasColumnName("hiring_date"); + }), + new ModelCodeGenerationOptions { UseDataAnnotations = false }, + code => + { + AssertFileContents( + @"using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; + +#nullable disable + +namespace TestNamespace +{ + public partial class TestDbContext : DbContext + { + public TestDbContext() + { + } + + public TestDbContext(DbContextOptions options) + : base(options) + { + } + + public virtual DbSet Employee { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { +#warning " + + DesignStrings.SensitiveInformationWarning + + @" + optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase""); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(entity => + { + entity.Property(e => e.Id).UseIdentityColumn(); + + entity.Property(e => e.HireDate) + .HasColumnType(""date"") + .HasColumnName(""hiring_date""); + }); + + OnModelCreatingPartial(modelBuilder); + } + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + } +} +", + code.ContextFile); + }, + model => + Assert.Equal("date", model.FindEntityType("TestNamespace.Employee").GetProperty("HireDate").GetConfiguredColumnType())); + } + private class TestCodeGeneratorPlugin : ProviderCodeGeneratorPlugin { public override MethodCallCodeFragment GenerateProviderOptions()