Search code examples
c#asp.netentity-frameworkasp.net-core-mvcasp.net-identity-2

How to add another tables to project with identity (EF, Code First)


I have a Model "Phone"

public class Phone
    {
        [Key]
        public Guid Id { get; set; }
        public string NameModel { get; set; }
        [MaxLength(100)]
        public int SerialNumber { get; set; }
    } 

End add this model to DBContext with Identity:

public class AppDbContext : IdentityDbContext<AppUser>
    {
        public AppDbContext (DbContextOptions<AppDbContext> options)
            : base(options)
        {
            Database.EnsureCreated();
        }

        public DbSet<Phone> Phones { get; set; }
    }

Next, I perform the database migration:

dotnet ef migrations add InitialMigration

and:

dotnet ef database update

But table "Phones" is not added to my database. What am I doing wrong?


Solution

  • EnsureCreated and Migrations don't work well together. If you're using Migrations, don't use EnsureCreated to initialize the schema.

    Source: https://learn.microsoft.com/en-us/ef/core/managing-schemas/ensure-created