Entity Framework Question

Mar 6 2018 2:02 AM
Hi I am trying the following in a migration file:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex("IX_CartLines_OrderID");
migrationBuilder.DropForeignKey(name: "FK_CartLines_Orders_OrderID", table: "CartLines");
migrationBuilder.DropPrimaryKey(name: "PK_Orders", table: "Orders");
migrationBuilder.DropTable(name: "Orders");
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
OrderID = table.Column<string>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Address1 = table.Column<string>(nullable: false),
Address2 = table.Column<string>(nullable: true),
Address3 = table.Column<string>(nullable: true),
City = table.Column<string>(nullable: false),
Country = table.Column<string>(nullable: false),
GiftWrap = table.Column<bool>(nullable: false),
Name = table.Column<string>(nullable: false),
State = table.Column<string>(nullable: false),
Zip = table.Column<string>(nullable: false),
userId = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.OrderID);
table.ForeignKey(
name: "FK_Orders_AspNetUsers_userId",
column: x => x.userId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.AddForeignKey(name: "FK_CartLines_Orders_OrderID", table: "CartLines", column: "OrderID", principalTable: "Orders", principalColumn: "OrderID", onUpdate: ReferentialAction.Cascade, onDelete: ReferentialAction.Cascade);
/* migrationBuilder.DropForeignKey(name: "FK_CartLines_Orders_OrderID", table: "CartLines");
migrationBuilder.DropPrimaryKey(name: "PK_Orders", table: "Orders");
migrationBuilder.AddColumn<int>(name: "test1", defaultValue: null, table: "Orders", nullable: true);
migrationBuilder.Sql("update Orders set test1 = OrderID");
migrationBuilder.DropColumn(name: "OrderID", table: "Orders");
migrationBuilder.RenameColumn(name: "test1", table: "Orders", newName: "OrderID");
migrationBuilder.AlterColumn<string>(name: "OrderID", type:"nvarchar(450)",table: "Orders", nullable: false, oldNullable: true, oldClrType: typeof(int));
migrationBuilder.DropIndex("IX_CartLines_OrderID");
migrationBuilder.AlterColumn<string>(name: "OrderID", table: "CartLines", type: "nvarchar(450)", oldClrType: typeof(int), nullable: false, oldNullable: true);
migrationBuilder.AddPrimaryKey(name: "PK_Orders", table: "Orders", column: "OrderID");
migrationBuilder.AddForeignKey(name: "FK_CartLines_Orders_OrderID", table: "CartLines", column: "OrderID", principalTable: "Orders", principalColumn: "OrderID", onUpdate: ReferentialAction.Cascade, onDelete: ReferentialAction.Cascade);
*/
/* migrationBuilder.DropForeignKey(
name: "FK_CartLines_Products_ProductId",
table: "CartLines");
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "Products",
nullable: false,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "Description",
table: "Products",
nullable: false,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "Category",
table: "Products",
nullable: false,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "OrderID",
table: "Orders",
nullable: false,
oldClrType: typeof(int))
.OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
migrationBuilder.AlterColumn<int>(
name: "ProductId",
table: "CartLines",
nullable: false,
oldClrType: typeof(int),
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "OrderID",
table: "CartLines",
nullable: true,
oldClrType: typeof(int),
oldNullable: true);
migrationBuilder.AddForeignKey(
name: "FK_CartLines_Products_ProductId",
table: "CartLines",
column: "ProductId",
principalTable: "Products",
principalColumn: "ProductId",
onDelete: ReferentialAction.Cascade); */
}
 
and I get the following message:
  Applying migration '20180301172108_ChangeOrderPKType'.
Applying migration '20180301172108_ChangeOrderPKType'.
System.ArgumentNullException: Value cannot be null.
Parameter name: name
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.Storage.RelationalSqlGenerationHelper.DelimitIdentifier(String name, String schema)
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(DropIndexOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(DropIndexOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Value cannot be null.
Parameter name: name
 
Any suggestion? 
 
 
 

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (2)