Skip to content

Commit

Permalink
RevEng: Remove ColumnType annotation from scaffolded code
Browse files Browse the repository at this point in the history
Since we put fluent API
  • Loading branch information
smitpatel committed Sep 15, 2020
1 parent 6c3eb88 commit 0121136
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>("Id");
x.Property<DateTime>("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<TestDbContext> options)
: base(options)
{
}
public virtual DbSet<Employee> 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<Employee>(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()
Expand Down

0 comments on commit 0121136

Please sign in to comment.