.NET Core 2.0中为ApplicationUser新增字段后迁移报错求助
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 caseThen delete the existing
UserTableUpdatemigration file, re-runAdd-Migration UserTableUpdate, and finish withUpdate-Database.Option 2: Manually edit the migration file
Open the generatedUserTableUpdatemigration file and add a default value in theUp()method:migrationBuilder.AddColumn<bool>( name: "Deactivated", table: "AspNetUsers", nullable: false, defaultValue: false); // Add this line to set a defaultSave the file and run
Update-Databaseagain.
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
__EFMigrationsHistorytable to confirm theInitialmigration was successfully applied. - If the
UserTableUpdatemigration partially ran and caused an error, roll back to theInitialstate with:
Then delete theUpdate-Database InitialUserTableUpdatemigration file, re-generate it withAdd-Migration UserTableUpdate, and runUpdate-Databaseagain.
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 includesDeactivated:[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
Deactivatedfield 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.jsonis 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

