为什么会出现此错误:
“ ApplicationDbContext”的部分声明不能指定不同的基类
请在下面查看我的代码:
using System;
using System.ComponentModel.DataAnnotations;
using IssueTracker.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace IssueTracker.Data
{
public class ApplicationUser: IdentityUser
{
[Required]
[MaxLength(100)]
public string FistName { get; set; }
[MaxLength(100)]
public string MiddleName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
public bool IsActivated { get; set; } = false;
public DateTime DateAdded { get; set; } = DateTime.Now;
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}
// Creating the roles
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>().ToTable("User");
builder.Entity<IdentityRole>().ToTable("Role");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim");
builder.Entity<IdentityUserToken<string>>().ToTable("UserToken");
builder.Entity<IdentityRole>().HasData(
new { Id = "1", name="Admin", NoralizeName = "ADMIN" },
new { Id = "2", name="Customer", NoralizeName = "CUSTOMER" },
new { Id = "3", name="Moderator", NoralizeName = "MODERATOR" }
);
}
public DbSet<ProductModel> Products { get; set; }
}
}
我正在使用Identity Framework并尝试重命名默认表名称。
如果我使用这个:
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}
...
我没有收到任何错误,但我无法重命名
builder.Entity()。ToTable(“ User”);
这只是给我AspNetUsers
您能帮我吗?谢谢。
编辑 添加建议的解决方案后,我仍然无法将AspNetUser重命名为User。请查看下面的迁移:
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
UserId = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false),
Discriminator = table.Column<string>(nullable: false),
FistName = table.Column<string>(maxLength: 100, nullable: true),
MiddleName = table.Column<string>(maxLength: 100, nullable: true),
LastName = table.Column<string>(maxLength: 100, nullable: true),
IsActivated = table.Column<bool>(nullable: true),
DateAdded = table.Column<DateTime>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUsers", x => x.UserId);
});