簡體   English   中英

遷移中的實體框架 7 復合鍵

[英]Entity Framework 7 composite keys in migrations

我第一次嘗試 MVC 6、Entity Framework 7 和 ASP.NET Identity。 我剛剛生成了一個默認項目,我正在根據以下教程更改登錄過程以支持租戶帳戶。

https://www.scottbrady91.com/ASPNET-Identity/Quick-and-Easy-ASPNET-Identity-Multitenancy

問題是,當我嘗試更改初始遷移以支持 AspNetUsers 表上的復合鍵 (Id,TenentId) 時,它只是失敗並顯示通用消息。 遷移代碼如下。

migrationBuilder.CreateTable(
                name: "AspNetUsers",
                columns: table => new
                {
                    Id = table.Column<string>(nullable: false),
                    TenantId = table.Column<string>(nullable: false),
                    AccessFailedCount = table.Column<int>(nullable: false),
                    ConcurrencyStamp = table.Column<string>(nullable: true),
                    Email = table.Column<string>(nullable: true),
                    EmailConfirmed = table.Column<bool>(nullable: false),
                    LockoutEnabled = table.Column<bool>(nullable: false),
                    LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                    NormalizedEmail = table.Column<string>(nullable: true),
                    NormalizedUserName = table.Column<string>(nullable: true),
                    PasswordHash = table.Column<string>(nullable: true),
                    PhoneNumber = table.Column<string>(nullable: true),
                    PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                    SecurityStamp = table.Column<string>(nullable: true),
                    TwoFactorEnabled = table.Column<bool>(nullable: false),
                    UserName = table.Column<string>(nullable: true)                    
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ApplicationUser", x => new { x.Id, x.TenantId });
                });

我也嘗試了以下代碼,但它也不起作用。

constraints: table =>
                    {
                        table.PrimaryKey("PK_ApplicationUser", x => x.Id);     
                        table.PrimaryKey("PK_ApplicationUser", x => x.TenantId);    
                    });

如果我將其保留為非復合鍵,遷移將成功執行。

constraints: table =>
                {
                    table.PrimaryKey("PK_ApplicationUser", x => x.Id);
                });

我試圖在兩列上定義一個唯一的約束,但我無法找到一種方法來做到這一點。

有沒有人對此有任何想法?

Identity 表的任何自定義都需要通過創建 Identity 模型類的派生類和/或通過從IdentityDbContext類創建派生類來完成。

但我不會嘗試更改 Identity 表的主鍵結構。 我嚴重懷疑這是否可行,如果確實如此,則無法保證它會正常工作。

為了讓您指向正確的方向,以下是我修改 IdentityUser 類(AspNetUsers 表)的方法。 我添加了一些屬性來存儲我從 Active Directory 中檢索到的數據。

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }

    [Required]
    public bool Active { get; set; }
}

這樣做需要您從使用上述模型類的IdentityDbContext類創建派生類。 在我的中,我重命名了 Identity 表。

public class MyDbContext : IdentityDbContext<ApplicationUser>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        //Rename the ASP.NET Identity tables.
        builder.Entity<ApplicationUser>().ToTable("User");
        builder.Entity<IdentityRole>().ToTable("Role");
        builder.Entity<ApplicationRole>().ToTable("Role");
        builder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");
        builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
        builder.Entity<IdentityRoleClaim<string>>().ToTable("UserRoleClaim");
    }
}

然后在 Startup.cs 的 ConfigureServices() 方法中

//Add Identity services.
services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<MyDbContext>()  
    .AddDefaultTokenProviders();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM