0

我不知道为什么会这样。我有继承自 IdentityDbContext 的 DbContext:

public class TestContext : IdentityDbContext<ApplicationUser>
    {
        public TestContext()
            : base("name=DomainModelContainer")
        {
        }

        //   protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //  {
        //       throw new UnintentionalCodeFirstException();
        //   }

        public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
        public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
        public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
        public virtual DbSet<AspNetUser> AspNetUsers { get; set; }

        public virtual DbSet<Blog> Blogs { get; set; }
        public virtual DbSet<Category> Categories { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
            modelBuilder.Entity<AspNetUserLogin>().HasKey<string>(l => l.UserId);
            modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
            modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
            base.OnModelCreating(modelBuilder);
            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

所以当我尝试执行简单的代码时。就像是

var testContext = new TestContext();
testContext.AspNetRoles.Count();

我得到一个例外Invalid object name 'dbo.AspNetRoles1'

为什么 EF 决定在表名中添加“1”?重点是什么?

4

0 回答 0