我试图排除一些默认带有 Identity 的表,但我收到每个表的警告:
The entity type 'IdentityUserToken' was first mapped explicitly and then ignored. Consider not mapping the entity type in the first place.
这是我的程序.cs:
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("APIContextConnection") ?? throw new InvalidOperationException("Connection string 'APIContextConnection' not found.");
builder.Services.AddDbContext<APIContext>(options => options.UseNpgsql(connectionString));
builder.Services.AddIdentity<APIUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<APIContext>();
// Add services to the container.
ServiceContainer.AddServices(builder.Services);
builder.Services.AddScoped<ISeeder, Seeder>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure JWT authentication
var key = builder.Configuration["JWT:Key"];
if (string.IsNullOrEmpty(key))
throw new InvalidOperationException("JWT key not found.");
builder.Services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JWT:Issuer"],
ValidAudience = builder.Configuration["JWT:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
}
Seed();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
void Seed()
{
using var scope = app.Services.CreateScope();
var dbSeeder = scope.ServiceProvider.GetRequiredService<ISeeder>();
dbSeeder.Seed();
}
这是我的背景:
public class APIContext(DbContextOptions<APIContext> options) : IdentityDbContext<APIUser>(options)
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<APIUser>().ToTable("User");
builder.Entity<IdentityRole>().ToTable("Role");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Ignore<IdentityUserToken<string>>();
builder.Ignore<IdentityUserClaim<string>>();
builder.Ignore<IdentityUserLogin<string>>();
builder.Ignore<IdentityRoleClaim<string>>();
}
// Entities
public DbSet<APIUser> User { get; set; }
}
我尝试将
base.OnModelCreating(builder);
放在所有 builder.Ignore<>
下面,但没有成功。
我实际上没有收到任何错误,但每次运行迁移时我都会收到此警告,这非常烦人:D
注意:我通过从
DbContext
继承上下文来修复它,而不是从 IdentityDbContext
。
这是结果:
public class APIContext(DbContextOptions<APIContext> options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Ignore<IdentityUserToken<string>>();
builder.Ignore<IdentityUserClaim<string>>();
builder.Ignore<IdentityUserLogin<string>>();
builder.Ignore<IdentityRoleClaim<string>>();
builder.Entity<APIUser>()
.ToTable("User");
builder.Entity<IdentityRole>()
.ToTable("Role");
builder.Entity<IdentityUserRole<string>>()
.ToTable("UserRole")
.HasKey(x => new { x.UserId, x.RoleId });
}
// Entities
public DbSet<APIUser> User { get; set; }
public DbSet<IdentityRole> Role { get; set; }
public DbSet<IdentityUserRole<string>> UserRole { get; set; }
}