You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 2.0中为ApplicationUser新增字段后迁移报错求助

Troubleshooting Your ApplicationUser Migration Error

Hey there! Let's work through this migration issue you're hitting when adding the Deactivated boolean field to your ApplicationUser model. Since your error message got cut off (starting with "Application star..."), I'll walk through the most common culprits and fixes for this scenario in .NET Core 2.0 MVC.

1. Missing Default Value for the New Boolean Field

The most common issue here is that your existing user records don't have a value to populate the new Deactivated field with—SQL Server will throw an error because it can't assign a null to a non-nullable boolean column.

Fixes:

  • Option 1: Set a default value directly in the model
    Update your ApplicationUser model to include a default value for the new field:

    public bool Deactivated { get; set; } = false; // Use true if that makes more sense for your use case
    

    Then delete the existing UserTableUpdate migration file, re-run Add-Migration UserTableUpdate, and finish with Update-Database.

  • Option 2: Manually edit the migration file
    Open the generated UserTableUpdate migration file and add a default value in the Up() method:

    migrationBuilder.AddColumn<bool>(
        name: "Deactivated",
        table: "AspNetUsers",
        nullable: false,
        defaultValue: false); // Add this line to set a default
    

    Save the file and run Update-Database again.

2. Migration Conflicts or Partial Application

Sometimes migrations can get stuck if a previous migration wasn't fully applied, or the generated migration file has conflicting logic.

Fixes:

  • First, check your database's __EFMigrationsHistory table to confirm the Initial migration was successfully applied.
  • If the UserTableUpdate migration partially ran and caused an error, roll back to the Initial state with:
    Update-Database Initial
    
    Then delete the UserTableUpdate migration file, re-generate it with Add-Migration UserTableUpdate, and run Update-Database again.

3. Double-Check Model Binding & Controller Setup

You mentioned updating the Bind method, but it's worth verifying to rule out any downstream issues (though this rarely causes migration errors):

  • Confirm your ApplicationUsersController's Bind attribute includes Deactivated:
    [Bind("Id,UserName,Email,Deactivated,...")] // Ensure Deactivated is in this list
    public ApplicationUser ApplicationUser { get; set; }
    
  • If you have Create/Edit views for users, add a checkbox for the Deactivated field to make sure the value saves correctly later:
    <div class="form-group">
        <label asp-for="ApplicationUser.Deactivated" class="control-label"></label>
        <input asp-for="ApplicationUser.Deactivated" class="form-check-input" />
        <span asp-validation-for="ApplicationUser.Deactivated" class="text-danger"></span>
    </div>
    

4. Database Permissions or Connection String Issues

If the error relates to database access, double-check:

  • Your connection string in appsettings.json is pointing to the correct database.
  • The account running your application (or the application pool identity if deployed) has permissions to alter tables in the database.

If you can share the full error message, I can narrow this down even further—but these steps should resolve the most common issues you're facing.

内容的提问来源于stack exchange,提问作者user6383418

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:34:23