Skip to content

Commit

Permalink
Don't use IProperty.Relational().ColumnType when we want only explici…
Browse files Browse the repository at this point in the history
…tly configured values

Fixes #9344
  • Loading branch information
bricelam committed Aug 23, 2017
1 parent 4fc88e8 commit d3bc450
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
lines.Add($".{nameof(RelationalPropertyBuilderExtensions.HasColumnName)}({_cSharpUtilities.DelimitString(columnName)})");
}

var columnType = property.Relational().ColumnType;
var columnType = property.GetConfiguredColumnType();

if (columnType != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void GenerateKeyAttribute(IProperty property)
private void GenerateColumnAttribute(IProperty property)
{
var columnName = property.Relational().ColumnName;
var columnType = property.Relational().ColumnType;
var columnType = property.GetConfiguredColumnType();

var delimitedColumnName = columnName != null && columnName != property.Name ? CSharpUtilities.DelimitString(columnName) : null;
var delimitedColumnType = columnType != null ? CSharpUtilities.DelimitString(columnType) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected virtual void ValidateDataTypes([NotNull] IModel model)
{
foreach (var property in entityType.GetDeclaredProperties())
{
var dataType = property.Relational().ColumnType;
var dataType = property.GetConfiguredColumnType();
if (dataType != null)
{
TypeMapper.ValidateTypeName(dataType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ public static class RelationalPropertyExtensions
/// </summary>
public static string FormatColumns([NotNull] this IEnumerable<IProperty> properties)
=> "{" + string.Join(", ", properties.Select(p => "'" + p.Relational().ColumnName + "'")) + "}";

/// <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>
public static string GetConfiguredColumnType([NotNull] this IProperty property)
=> (string)property[RelationalAnnotationNames.ColumnType];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ protected virtual IEnumerable<MigrationOperation> Diff(
&& s.GetMaxLength() == t.GetMaxLength()
&& s.IsColumnNullable() == t.IsColumnNullable()
&& s.IsUnicode() == t.IsUnicode()
&& sAnnotations.ColumnType == tAnnotations.ColumnType
&& s.GetConfiguredColumnType() == t.GetConfiguredColumnType()
&& sAnnotations.ComputedColumnSql == tAnnotations.ComputedColumnSql
&& Equals(sAnnotations.DefaultValue, tAnnotations.DefaultValue)
&& sAnnotations.DefaultValueSql == tAnnotations.DefaultValueSql;
Expand Down Expand Up @@ -712,7 +712,7 @@ private ColumnOperation Initialize(
bool inline = false)
{
columnOperation.ClrType = property.ClrType.UnwrapNullableType().UnwrapEnumType();
columnOperation.ColumnType = annotations.ColumnType;
columnOperation.ColumnType = property.GetConfiguredColumnType();
columnOperation.MaxLength = property.GetMaxLength();
columnOperation.IsUnicode = property.IsUnicode();
columnOperation.IsRowVersion = property.ClrType == typeof(byte[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Migrations.Internal
Expand Down Expand Up @@ -760,6 +763,46 @@ public void Rename_property_and_column()
});
}

[Fact]
public void Rename_property_and_column_when_snapshot()
{
// NB: No conventions to simulate the snapshot.
var sourceModelBuilder = new ModelBuilder(new ConventionSet());
sourceModelBuilder.Entity(
typeof(Crab).FullName,
x =>
{
x.ToTable("Crab");
x.Property<string>("CrabId")
.ValueGeneratedOnAdd();
x.HasKey("CrabId");
});

var targetModelBuilder = CreateModelBuilder();
targetModelBuilder.Entity<Crab>();

// NB: Call Validate() so ModelBuilt conventions are applied.
targetModelBuilder.GetInfrastructure().Metadata.Validate();

var modelDiffer = RelationalTestHelpers.Instance.CreateContextServices()
.GetRequiredService<IMigrationsModelDiffer>();
var operations = modelDiffer.GetDifferences(sourceModelBuilder.Model, targetModelBuilder.Model);

Assert.Equal(1, operations.Count);

var operation = Assert.IsType<RenameColumnOperation>(operations[0]);
Assert.Equal("Crab", operation.Table);
Assert.Equal("CrabId", operation.Name);
Assert.Equal("Id", operation.NewName);
}

private class Crab
{
public string Id { get; set; }
}

[Fact]
public void Add_custom_value_generator()
{
Expand Down

0 comments on commit d3bc450

Please sign in to comment.