3

How can I remove ASP.NET Identity tables from a database? Right now I have the following tables added to my database :

  • __MigrationHistory
  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

I know that I could write something like the following :

DROP TABLE __MigrationHistory
DROP TABLE AspNetUserLogins
DROP TABLE AspNetUserRoles
DROP TABLE AspNetUserClaims
DROP TABLE AspNetRoles
DROP TABLE AspNetUsers

However I was wondering whether this is the right approach and if there is more elegant method of removing ASP.NET Identity tables and any other potential traces which I might have left out.

MHOOS
  • 5,146
  • 11
  • 39
  • 74
  • What's wrong with simply dropping the tables? – mason Nov 26 '14 at 15:22
  • Nothing is obviously wrong but I was asking whether this is the only method possible. – MHOOS Nov 26 '14 at 15:25
  • Any method is going to ultimately run these same drop commands. How did you create the tables? By running the commands directly against the server? Then the reverse is appropriate. I'm not familiar with setting up Identity, but did you call some sort of `CreateTablesOnDatabase()` function? If so, I imagine there's an equivalent `DropTablesonDatabase()` or you could see what the create function does to create the reverse. – mason Nov 26 '14 at 15:31
  • I use EF6 Code First approach and I derive my database from IdentityDbContext which automatically takes care of creating relevant tables plus the ones I explicitly specify. – MHOOS Nov 26 '14 at 15:34
  • If you were using Database First, [here's the SQL](https://github.com/kriasoft/AspNet.Identity/tree/master/src/KriaSoft.AspNet.Identity.Database/Tables) you'd execute to create the tables. Perhaps you should try the reverse. – mason Nov 26 '14 at 15:39
  • I think we are not on the same page on this. I am looking for some ASP.NET Identity or entity framework solution(if any) and it is becoming apparent that removing those tables using SQL statement is the only possible option. Thanks for the effort though. – MHOOS Nov 26 '14 at 15:46
  • We are on the same page. I just wanted to point out that to undo things you've done, you simply do the opposite in reverse. – mason Nov 26 '14 at 15:49
  • How do you Undo this => ApplicationDbContext : IdentityDbContext – MHOOS Nov 26 '14 at 15:51
  • That's not what *actually* made your tables is it? You're using Code Migrations. Therefore, when you executed the migration it automagicallty made your tables by running a sequence of `create table` statements. So you can try [rolling back the migrations](http://stackoverflow.com/questions/11904571/ef-migrations-rollback-last-applied-migration) and see if it deletes the tables. Or you can manually delete them. Note that I'm trying to teach you the concept of how to undo something, and an important part of that is understanding how it got the way it is in the first place. – mason Nov 26 '14 at 15:57
  • Is there anything else which comes to your mind that you think you could potential teach me today because I am learning great level of cool reverse engineering stuff from you? – MHOOS Nov 26 '14 at 16:02
  • Lots my young apprentice. (apparently you have 10 years on me) You could probably teach me a thing or two too. Unfortunately, you'll just have to make a new question with a new problem if you want help on other things. – mason Nov 26 '14 at 16:04
  • 1
    I like your confidence but I have an advice for you as well. Don't be "over-confident". "Over confidence" is lethal. – MHOOS Nov 26 '14 at 16:08
  • haha you guys are funny! – Pascal Aug 24 '15 at 20:07

2 Answers2

4

You have to remove following tables from the database.

DROP TABLE __EFMigrationsHistory
Drop Table AspNetRoleClaims
DROP TABLE AspNetUserLogins
DROP TABLE AspNetUserRoles
DROP TABLE AspNetUserClaims
Drop Table AspNetUserTokens
DROP TABLE AspNetRoles
DROP TABLE AspNetUsers

Also you need to drop Identity schema, if it's already exists orphan in your database.

Sadegh
  • 4,181
  • 9
  • 45
  • 78
Osman Taskiran
  • 359
  • 3
  • 9
1

Just a note: __MigrationHistory is not part of Identity framework - it is table that stores information about DB state for EF-Migrations. If you would like to preserve the migration state and keep using the migrations, don't do anything to this table (unless you know what you are doing)

And I'll point out (seems like you have your answers already) that if you have created these tables by migration, you can roll back to "state-zero" by

update-database -target:0

This will run all your migrations back to the start and you'll be left with one table __MigrationHistory

trailmax
  • 34,305
  • 22
  • 140
  • 234