So, I simply changed the column definition from:
[Display(Name = "Bill of Lading")]
[Required]
[StringLength(80)]
public string BillOfLading { get; set; }
To:
[Display(Name = "Bill of Lading")]
public int BillOfLading { get; set; }
I patched up my code so it would compile with the new type, and I added a migration, which looked like this:
public partial class TransloadingDetailBillOfLadingToInt : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "BillOfLading",
table: "TransloadingDetails",
type: "int",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(80)",
oldMaxLength: 80);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "BillOfLading",
table: "TransloadingDetails",
type: "nvarchar(80)",
maxLength: 80,
nullable: false,
oldClrType: typeof(int),
oldType: "int");
}
}
I was able to run update-database on a smaller, demo database we have. On my first attempt, I had a column value that could not be converted. In that instance, I got an error and no data was lost. A good sign!
After correcting the data, I ran it again and the text columns were converted to integers. All the existing data was correctly converted. And then the same thing on my main database.
So, at least in the case of converting a text column to an integer column, where the data can be converted, this works just as you'd want it to work.
intand adding a migration?