0

I don't have any idea why it happens. I have DbContext which inherits from IdentityDbContext:

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>();
        }
    }

So when I try to execute simple code. Something like

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

I get an exception Invalid object name 'dbo.AspNetRoles1'

Why EF decides to add "1" to table name? What is the point?

Neir0
  • 12,849
  • 28
  • 83
  • 139
  • When you view the database generated by Code First, does it already have an AspNetRoles table? – m.casey Jul 16 '14 at 20:35
  • @m.casey Actually I use Model First – Neir0 Jul 16 '14 at 20:47
  • Oh, in that case you check the name of your AspNetRoles entity in your EDMX file. Does it say AspNetRoles1? There should be two properties: one with the name of the entity (class) and the other with the name of the table to which it maps. – m.casey Jul 17 '14 at 00:43
  • @m.casey No, I searched AspNetRoles1 everywhere but with no luck – Neir0 Jul 17 '14 at 10:55

0 Answers0