Efcore: Old column type is incorrect in migrations

Created on 27 Jan 2020  路  4Comments  路  Source: dotnet/efcore

To get the store type of the old column, we call GetColumnType which looks only at the property's ClrType and ignores the operation's ClrType. Since the property already has the new ClrType, we get the new store type as well, and the old and new store types are always the same.

In the SqlServer case, this means that when changing from long to int, narrowed isn't true and we don't rebuild indexes. Repro in MigrationsSqlServerTest:

```c#
[ConditionalFact]
public async Task Alter_column_change_type_with_indexes()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property("Id");
e.HasIndex("Id");
}),
builder => builder.Entity(
"People", e =>
{
e.Property("Id");
e.HasIndex("Id");
}),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns);
Assert.Equal("int", column.StoreType);
});

AssertSql(
    @"");

}
```

/cc @bricelam

area-migrations closed-fixed type-bug

All 4 comments

I wonder if this is the root cause of https://github.com/dotnet/efcore/issues/12586

BTW @bricelam, would be good to add Alter_column_change_type_with_indexes and other similar tests to MigrationTestBase as it seems very relevant for other providers too (e.g. https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/678).

PS Am so fortunate to be doing PG... You can just alter a column's type from bigint to int, even if it has an index, and PG will just do it...

This appears to be fixed. Probably by #20305

Confirmed that this is fixed - my migrations SQL is starting to look tight!

Was this page helpful?
0 / 5 - 0 ratings