.NET Core 2.0向ApplicationUser添加新字段

时间:2018-02-08 17:46:07

标签: c# entity-framework asp.net-core database-schema

我有一个.NET Core 2.0 MVC项目。我最初向ApplicationUser添加了4个字段 - 然后在Nuget Package Manager Console中,我运行了Add-Migration Initial。我已经向数据库添加了许多用户,现在我需要向该模型添加另一个字段。我添加了布尔字段" Deactivated"在ApplicationUser模型中,我更新了ApplicationUsersController以将新字段添加到Bind方法

Task<IActionResult> Create([Bind("Name,DateCreated,DateUpdated,LastLogin,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount,Deactivated")] ApplicationUser applicationUser) 

然后我运行Add-Migration UserTableUpdate和Update-Database

我收到错误

  

应用程序启动异常:System.AggregateException:发生了一个或多个错误。 (无效的列名称&#39;已停用&#39;。)---&gt; System.Data.SqlClient.SqlException:列名无效&#39;已停用&#39;。

我尝试手动将该列添加到数据库,然后尝试将该列重新添加为重复项并抛出错误,说它无法添加具有相同名称的列。我哪里出错了?

ApplicationDBContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using TestProject.Models;

namespace TestProject.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }

        public DbSet<TestProject.Models.CustomErrorLogModel> CustomErrorLogModel { get; set; }

        public DbSet<TestProject.Models.ApplicationUser> ApplicationUser { get; set; }
    }
}

UserTableUpdate 迁移

using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

namespace TestProject.Data.Migrations
{
    public partial class UserTableUpdate : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "Deactivated",
                table: "AspNetUsers",
                nullable: true,
                defaultValue: false);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Deactivated",
                table: "AspNetUsers");
        }
    }
}

0 个答案:

没有答案