问题
Is there a way to only specify one index when overriding the OnModelCreating
function in our context folder?
I added:
builder.Entity<ApplicationUser>()
.HasIndex(e => e.Id);
Which does add that as an index, but it looks like a few other properties have been made an index as well.
Is there a way to make it the only index?
The reason why I want to specify one index is: my migration file theres a bunch of dropForeignKeys, RenameTable, CreateIndex for things that i haven't touched. It happens to try to drop an index that Isn't even in my table which causes my migration to fail. Which is why I want to only specify one index
Context File: `
public class MyContext : IdentityDbContext
{
public MyContext(DbContextOptions<MyContext> options) : base(options)
{ }
// Snippet, I only added this InboxNotification so I expected to only see a create and corresponding drop table
public virtual DbSet<InboxNotification> InboxNotifications { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationRole>()
.HasMany(e => e.Claims)
.WithOne()
.HasForeignKey(e => e.RoleId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ApplicationRole>()
.HasMany(e => e.Users)
.WithOne()
.HasForeignKey(e => e.RoleId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ApplicationUser>()
.HasMany(e => e.Claims)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ApplicationUser>()
.HasMany(e => e.Logins)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ApplicationUser>()
.HasMany(e => e.Roles)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
// // Maps the .NET types to our db
// builder.Entity<IdentityUser>().ToTable("AspNetUsers")
// .HasKey(x => x.Id);
// builder.Entity<IdentityRole>().ToTable("AspNetRoles")
// .HasKey(x => x.Id);
builder.Entity<IdentityUserRole<string>>().ToTable("AspNetUserRoles")
.HasKey(x => new { x.UserId, x.RoleId });
builder.Entity<IdentityUserClaim<string>>().ToTable("AspNetUserClaims")
.HasKey(x => x.Id);
builder.Entity<IdentityUserLogin<string>>().ToTable("AspNetUserLogins")
.HasKey(x => new { x.UserId, x.ProviderKey });
builder.Entity<IdentityUserToken<string>>().ToTable("AspNetUserTokens")
.HasKey(x => new { x.UserId, x.LoginProvider, x.Name });
builder.Entity<IdentityRoleClaim<string>>().ToTable("AspNetRoleClaims")
.HasKey(x => x.Id);
builder.Entity<ApplicationUserToken>().ToTable("UserTokens")
.HasKey(x => x.Id);
builder.Entity<Team>()
.HasIndex(t => new
{
t.Name,
t.LeagueId
})
.HasName("Team_Name")
.IsUnique();
builder.Entity<Club>()
.HasIndex(c => c.Name)
.IsUnique();
builder.Entity<TeamMatch>()
.HasIndex(tm => new
{
tm.Name,
tm.TeamId
})
.IsUnique();
builder.Entity<TeamMatch>()
.HasIndex(tm => new
{
tm.Slug,
tm.TeamId
})
.IsUnique();
builder.Entity<Practice>()
.HasIndex(p => new { p.Name, p.TeamId })
.IsUnique();
builder.Entity<Practice>()
.HasIndex(p => new { p.Slug, p.TeamId })
.IsUnique();
builder.Entity<SocialPlayFriend>()
.HasKey(t => new { t.PlayerId, t.SocialPlayPreferenceId });
builder.Entity<SocialPlayFriend>()
.HasOne(f => f.Preference)
.WithMany(spp => spp.Friends)
.HasForeignKey(f => f.SocialPlayPreferenceId);
builder.Entity<SocialPlayFriend>()
.HasOne(f => f.Friend)
.WithMany(p => p.Friends)
.HasForeignKey(f => f.PlayerId);
builder.Entity<SocialPlayClub>()
.HasKey(c => new { c.SocialPlayPreferenceId, c.ClubId });
builder.Entity<SocialPlayClub>()
.HasOne(f => f.Preference)
.WithMany(spp => spp.Clubs)
.HasForeignKey(f => f.SocialPlayPreferenceId);
builder.Entity<SocialPlayClub>()
.HasOne(f => f.Club)
.WithMany(c => c.SocialPlayers)
.HasForeignKey(f => f.ClubId);
}
}
`
Migration File: `
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace MyAPI.Migrations
{
public partial class InboxNotification : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ApplicationUserToken_AspNetUsers_UserId",
table: "ApplicationUserToken");
migrationBuilder.DropIndex(
name: "Team_Name",
table: "Teams");
migrationBuilder.DropIndex(
name: "IX_TeamMatches_Name_TeamId",
table: "TeamMatches");
migrationBuilder.DropIndex(
name: "IX_TeamMatches_Slug_TeamId",
table: "TeamMatches");
migrationBuilder.DropIndex(
name: "IX_Practices_Name_TeamId",
table: "Practices");
migrationBuilder.DropIndex(
name: "IX_Practices_Slug_TeamId",
table: "Practices");
migrationBuilder.DropIndex(
name: "IX_Players_UserId",
table: "Players");
migrationBuilder.DropIndex(
name: "IX_Clubs_Name",
table: "Clubs");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "AspNetUsers");
migrationBuilder.DropPrimaryKey(
name: "PK_ApplicationUserToken",
table: "ApplicationUserToken");
migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");
migrationBuilder.RenameTable(
name: "ApplicationUserToken",
newName: "UserTokens");
migrationBuilder.RenameIndex(
name: "IX_ApplicationUserToken_UserId",
table: "UserTokens",
newName: "IX_UserTokens_UserId");
migrationBuilder.AddColumn<string>(
name: "Discriminator",
table: "AspNetRoles",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
migrationBuilder.AddPrimaryKey(
name: "PK_UserTokens",
table: "UserTokens",
column: "Id");
migrationBuilder.CreateTable(
name: "InboxNotifications",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
DataId = table.Column<string>(type: "nvarchar(max)", nullable: true),
EventIdent = table.Column<string>(type: "nvarchar(max)", nullable: true),
Message = table.Column<string>(type: "nvarchar(max)", nullable: true),
Status = table.Column<int>(type: "int", nullable: false),
TeamIdent = table.Column<string>(type: "nvarchar(max)", nullable: true),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
Type = table.Column<int>(type: "int", nullable: false),
UserId = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_InboxNotifications", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "Team_Name",
table: "Teams",
columns: new[] { "Name", "LeagueId" },
unique: true,
filter: "[Name] IS NOT NULL AND [LeagueId] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_TeamMatches_Name_TeamId",
table: "TeamMatches",
columns: new[] { "Name", "TeamId" },
unique: true,
filter: "[Name] IS NOT NULL AND [TeamId] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_TeamMatches_Slug_TeamId",
table: "TeamMatches",
columns: new[] { "Slug", "TeamId" },
unique: true,
filter: "[Slug] IS NOT NULL AND [TeamId] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_Practices_Name_TeamId",
table: "Practices",
columns: new[] { "Name", "TeamId" },
unique: true,
filter: "[Name] IS NOT NULL AND [TeamId] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_Practices_Slug_TeamId",
table: "Practices",
columns: new[] { "Slug", "TeamId" },
unique: true,
filter: "[Slug] IS NOT NULL AND [TeamId] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_Players_UserId",
table: "Players",
column: "UserId",
unique: true,
filter: "[UserId] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_Clubs_Name",
table: "Clubs",
column: "Name",
unique: true,
filter: "[Name] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "NormalizedUserName",
unique: true,
filter: "[NormalizedUserName] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true,
filter: "[NormalizedName] IS NOT NULL");
migrationBuilder.AddForeignKey(
name: "FK_AspNetUserTokens_AspNetUsers_UserId",
table: "AspNetUserTokens",
column: "UserId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_UserTokens_AspNetUsers_UserId",
table: "UserTokens",
column: "UserId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_AspNetUserTokens_AspNetUsers_UserId",
table: "AspNetUserTokens");
migrationBuilder.DropForeignKey(
name: "FK_UserTokens_AspNetUsers_UserId",
table: "UserTokens");
migrationBuilder.DropTable(
name: "InboxNotifications");
migrationBuilder.DropPrimaryKey(
name: "PK_UserTokens",
table: "UserTokens");
migrationBuilder.DropIndex(
name: "Team_Name",
table: "Teams");
migrationBuilder.DropIndex(
name: "IX_TeamMatches_Name_TeamId",
table: "TeamMatches");
migrationBuilder.DropIndex(
name: "IX_TeamMatches_Slug_TeamId",
table: "TeamMatches");
migrationBuilder.DropIndex(
name: "IX_Practices_Name_TeamId",
table: "Practices");
migrationBuilder.DropIndex(
name: "IX_Practices_Slug_TeamId",
table: "Practices");
migrationBuilder.DropIndex(
name: "IX_Players_UserId",
table: "Players");
migrationBuilder.DropIndex(
name: "IX_Clubs_Name",
table: "Clubs");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "AspNetUsers");
migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");
migrationBuilder.DropColumn(
name: "Discriminator",
table: "AspNetRoles");
migrationBuilder.RenameTable(
name: "UserTokens",
newName: "ApplicationUserToken");
migrationBuilder.RenameIndex(
name: "IX_UserTokens_UserId",
table: "ApplicationUserToken",
newName: "IX_ApplicationUserToken_UserId");
migrationBuilder.AddPrimaryKey(
name: "PK_ApplicationUserToken",
table: "ApplicationUserToken",
column: "Id");
migrationBuilder.CreateIndex(
name: "Team_Name",
table: "Teams",
columns: new[] { "Name", "LeagueId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_TeamMatches_Name_TeamId",
table: "TeamMatches",
columns: new[] { "Name", "TeamId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_TeamMatches_Slug_TeamId",
table: "TeamMatches",
columns: new[] { "Slug", "TeamId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Practices_Name_TeamId",
table: "Practices",
columns: new[] { "Name", "TeamId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Practices_Slug_TeamId",
table: "Practices",
columns: new[] { "Slug", "TeamId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Players_UserId",
table: "Players",
column: "UserId",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Clubs_Name",
table: "Clubs",
column: "Name",
unique: true);
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "NormalizedUserName",
unique: true);
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true);
migrationBuilder.AddForeignKey(
name: "FK_ApplicationUserToken_AspNetUsers_UserId",
table: "ApplicationUserToken",
column: "UserId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}
`
来源:https://stackoverflow.com/questions/49762438/specify-only-one-index-with-fluent-api