CodeFirst-Adding column of type varchar to an existing table which has data, add-migration command generates wrong migration..
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
### Bug description
Hi,
We've scenario where we 've table definition and data insertion using HasData(..) as first level of migration. Table has just ID as int which is also PK and NAME having type varchar(100). First level of migration gets applied without any issue.
Now as 2nd level of migration, we require to add column ADDRESS having type varchar(200) and default value is configured as "BOMBAY". Now when we generate 2nd level of migration it generates:
migrationBuilder.UpdateData(
table: "EFCFTAB1s",
keyColumn: "ID",
keyValue: 3,
columns: new string[0],
values: new object[0]);
Here we can see columns must contain ADDRESS but it has new string[0], which causes wrong SQL generation and migration fails while getting applied to database.
We also tried adding column of type int which works fine without any issue..
### Your code
```csharp
Entity class:
public class EFCFTAB1
{
[Key]
public int ID { get; set; }
[MaxLength(100)]
public string NAME { get; set; }
//this we can uncomment in next level of migration to add column of type int
//public int PERSONALID { get; set; }
// Uncomment this one to reproduce the issue in second Or third level of migration
//[MaxLength(200)]
//public string ADDRESS { get; set; }
}
Entity configuration in Context class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity().HasKey(q => q.ID);
modelBuilder.Entity().Property(q => q.ID).HasColumnType("int");
//Uncomment following when you want to add column ADDRESS
//modelBuilder.Entity().Property(q => q.ADDRESS).HasDefaultValue("Bombay");
modelBuilder.Entity().HasData(
new EFCFTAB1 { ID = 1, NAME = "Laptop" },
new EFCFTAB1 { ID = 2, NAME = "Mouse" },
new EFCFTAB1 { ID = 3, NAME = "Mouse" },
new EFCFTAB1 { ID = 4, NAME = "Mouse" }
);
}
```
### Stack traces
```text
```
### Verbose output
```text
ALTER TABLE [EFCFTAB1s] ADD [ADDRESS] nvarchar(200) NOT NULL DEFAULT N'Bombay';
8/19/2025 06:20:22.398 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [EFCFTAB1s] ADD [ADDRESS] nvarchar(200) NOT NULL DEFAULT N'Bombay';
Creating DbCommand for 'ExecuteNonQuery'.
dbug: 8/19/2025 06:20:22.398 RelationalEventId.CommandCreating[20103] (Microsoft.EntityFrameworkCore.Database.Command)
Creating DbCommand for 'ExecuteNonQuery'.
Created DbCommand for 'ExecuteNonQuery' (0ms).
dbug: 8/19/2025 06:20:22.398 RelationalEventId.CommandCreated[20104] (Microsoft.EntityFrameworkCore.Database.Command)
Created DbCommand for 'ExecuteNonQuery' (0ms).
Initialized DbCommand for 'ExecuteNonQuery' (0ms).
dbug: 8/19/2025 06:20:22.398 RelationalEventId.CommandInitialized[20106] (Microsoft.EntityFrameworkCore.Database.Command)
Initialized DbCommand for 'ExecuteNonQuery' (0ms).
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
UPDATE [EFCFTAB1s] SET
WHERE [ID] = 1;
SELECT @@ROWCOUNT;
dbug: 8/19/2025 06:20:22.398 RelationalEventId.CommandExecuting[20100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
UPDATE [EFCFTAB1s] SET
WHERE [ID] = 1;
SELECT @@ROWCOUNT;
Failed executing DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
UPDATE [EFCFTAB1s] SET
WHERE [ID] = 1;
SELECT @@ROWCOUNT;
fail: 8/19/2025 06:20:22.429 RelationalEventId.CommandError[20102] (Microsoft.EntityFrameworkCore.Database.Command)
Failed executing DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
UPDATE [EFCFTAB1s] SET
WHERE [ID] = 1;
SELECT @@ROWCOUNT;
```
### EF Core version
9.0.8
### Database provider
Microsoft.EntityFrameworkCore.SqlServer
### Target framework
.NET 9 and .NET8
### Operating system
Windows 11
### IDE
Visual Studio 2022 Release
Contributor guide
Assessment
This issue has not been assessed yet.