EF5 Code First - Changing A Column Type With Migrations

后端 未结 3 1948
长发绾君心
长发绾君心 2020-11-30 02:33

I am new to EF5 Code First and I\'m tinkering with a proof-of-concept before embarking on a project at work.

I have initially created a model that looked something l

3条回答
  •  鱼传尺愫
    2020-11-30 02:56

    I know this doesn't apply directly to the question but could be helpful to someone. In my problem, I accidentally made a year field a datetime and I was trying to figure out how to delete all the data and then switch the data type to an int.

    When doing an add-migration, EF wanted to just update the column. I had to delete what they wanted to do and add my own code. I basically just dropped the column and added a new column. Here is what worked for me.

    protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "TestingPeriodYear",
                table: "ControlActivityIssue");
    
            migrationBuilder.AddColumn(
                name: "TestingPeriodYear",
                table: "ControlActivityIssue",
                nullable: true);
        }
    
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "TestingPeriodYear",
                table: "ControlActivityIssue");
    
            migrationBuilder.AddColumn(
                name: "TestingPeriodYear",
                table: "ControlActivityIssue",
                nullable: true);
        }
    

提交回复
热议问题