简体   繁体   English

如何在 asp.net-core 中使用代码优先迁移删除唯一约束

[英]How to Drop Unique Constraint with code-first migration in asp.net-core

I have a project for which I added an initial migration with a foreign key as follows:我有一个项目,我添加了一个带有外键的初始迁移,如下所示:

 public class ApplicationUser : IdentityUser 
 {
  public string RegistrarId { get; set; }
 
  [ForeignKey("RegistrarId")]
  public virtual ApplicationUser Registrar { get; set; }

 }

However, I realized that when I run the code-first migration, it is automatically creating a unique index on the Registrar column which is against my wish.但是,我意识到当我运行代码优先迁移时,它会自动在 Registrar 列上创建一个唯一索引,这违背了我的意愿。

 migrationBuilder.CreateIndex(
            name: "IX_AspNetUsers_RegistrarId",
            table: "AspNetUsers",
            column: "RegistrarId",
            unique: true,
            filter: "[RegistrarId] IS NOT NULL");

My question is, Is there a way to modify my class to remove this constraint with another migration.我的问题是,有没有办法修改我的类以通过另一个迁移删除此约束。

Below is what I have for the ApplicationUser entity onModelCreating下面是我对 ApplicationUser 实体 onModelCreating 所拥有的

 protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>(b =>
        {
            // Each User can have many UserClaims
            b.HasMany(e => e.Claims)
                .WithOne(e => e.User)
                .HasForeignKey(uc => uc.UserId)
                .IsRequired();

            // Each User can have many UserLogins
            b.HasMany(e => e.Logins)
                .WithOne(e => e.User)
                .HasForeignKey(ul => ul.UserId)
                .IsRequired();

            // Each User can have many UserTokens
            b.HasMany(e => e.Tokens)
                .WithOne(e => e.User)
                .HasForeignKey(ut => ut.UserId)
                .IsRequired();

            // Each User can have many entries in the UserRole join table
            b.HasMany(e => e.UserRoles)
                .WithOne(e => e.User)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();

            b.HasIndex(e => e.Email)
                .IsUnique();             
        });

My application is on ASP.NET-Core 3.1 with EF-Core 3.1 and SQLServer我的应用程序在 ASP.NET-Core 3.1 上,带有 EF-Core 3.1 和 SQLServer

Please help Thank you请帮忙谢谢

It is very likely that you have unique index configured in OnModelCreating .您很可能在OnModelCreating配置了唯一索引。 If you do, just flip it to false.如果这样做,只需将其翻转为 false。

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<ApplicationUser>()
        .HasIndex(x => x.RegistrarId)
        .IsUnique(false);

    base.OnModelCreating(builder);
}

It will generate "drop and create" index migration, but without unique attribute它将生成“删除和创建”索引迁移,但没有唯一属性

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropIndex(
        name: "IX_AspNetUsers_RegistrarId",
        table: "AspNetUsers");

    migrationBuilder.CreateIndex(
        name: "IX_AspNetUsers_RegistrarId",
        table: "AspNetUsers",
        column: "RegistrarId");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM