如何阻止 dotnet ef 迁移添加创建不必要的附加字段

how to stop dotnet ef migrations adding from creating unnecessary additional fields

这是一个只有一个 class 的准系统应用程序:AppUser 只有 (Id、FirstName、LastName、Phone1、Phone2、KnownAs、EmailAddress、Question、Answer 和 IsTrialMode。

没有其他表了!

DataContext is only:

    public class DataContext : DbContext
{
    public DataContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<AppUser> Users { get; set; }
}

当我执行 dotnet ef migrations add 命令时,迁移文件包含:

            migrationBuilder.CreateTable(
            name: "Users",
            columns: table => new
            {
                Id = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                LastName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                Phone1 = table.Column<string>(type: "nvarchar(max)", nullable: true),
                Phone2 = table.Column<string>(type: "nvarchar(max)", nullable: true),
                KnownAs = table.Column<string>(type: "nvarchar(max)", nullable: true),
                EmailAddress = table.Column<string>(type: "nvarchar(max)", nullable: true),
                Question = table.Column<string>(type: "nvarchar(max)", nullable: true),
                Answer = table.Column<string>(type: "nvarchar(max)", nullable: true),
                IsTrialMode = table.Column<bool>(type: "bit", nullable: false),
                UserName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                NormalizedUserName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
                NormalizedEmail = table.Column<string>(type: "nvarchar(max)", nullable: true),
                EmailConfirmed = table.Column<bool>(type: "bit", nullable: false),
                PasswordHash = table.Column<string>(type: "nvarchar(max)", nullable: true),
                SecurityStamp = table.Column<string>(type: "nvarchar(max)", nullable: true),
                ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true),
                PhoneNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
                PhoneNumberConfirmed = table.Column<bool>(type: "bit", nullable: false),
                TwoFactorEnabled = table.Column<bool>(type: "bit", nullable: false),
                LockoutEnd = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                LockoutEnabled = table.Column<bool>(type: "bit", nullable: false),
                AccessFailedCount = table.Column<int>(type: "int", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Users", x => x.Id);
            });

这显然不是我所期待的。
是什么原因造成的;如何让它停止?

提前致谢。 “查克”

根据您的迁移文件,附加属性似乎是 IdentityUser class 默认属性。

确保您的 AppUser 没有扩展 IdentityUserIdentityUser<T> class:

public class AppUser
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone1 { get; set; }
    public string Phone2 { get; set; }
    public string KnownAs { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
    public string EmailAddress { get; set; }
    public bool IsTrialMode { get; set; }
}