3

I've setup a .net core api app to host JWT token authentication using an asp identity database. That part works great. My problem is when I go to add custom classes, every migration I perform is trying to create the AspNet* tables again even though they exist. The migration is aware of my custom class (Test) and wants to create it, but the script it's trying to run dies on the AspNetRoles table (already exists error).

I create a new migration:

Add-Migration NewMigration

Then update:

Update-Database

Here is my ApplicationDBContext.cs:

public class ApplicationDBContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
    {
    }

    // This is my custom class
    public DbSet<Test> Test { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

I'm at a loss. I just need to do migrations and have EF Core ignore the AspNet identity tables.

Edit: Here is ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
}

Migrations folder:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jeffrey Bane
  • 592
  • 1
  • 10
  • 40
  • Can you show one of your classes inherited from those 7 built-in `ASP.NET Core Identity` entities, i.e. `ApplicationUser`? Also a screenshot of the migration folder and how you add this `ApplicationDbContext` to your main web project would be helpful. – David Liang Mar 22 '19 at 19:58
  • Edited to add ApplicationUser class (empty) and screenshot of the migration folder. I manually added the ApplicationDbContext to the Api project. – Jeffrey Bane Mar 22 '19 at 20:08
  • I manually created DbContext too. That should have no problem. Did you use `services.AddIdentity(...)` to register the identity service at Startup.cs? – David Liang Mar 22 '19 at 21:47
  • 1
    Did you by chance manipulated or deleted your `__MigrationHistory` table? Try [start clean](https://stackoverflow.com/a/16082497/2441442). I see no problems with you current code. – Christian Gollhardt Mar 23 '19 at 01:20
  • I deleted the db and started clean as Christian had suggested and everything is working as expected now. Something must have gotten out of sync somewhere. Luckily the database didn't have any data in it yet. I will vote this as the answer if you post it. – Jeffrey Bane Mar 25 '19 at 14:08

0 Answers0