简体   繁体   中英

How does ASP.NET MVC Integrated Identity projects create tables once and only once?

When you create a new ASP.NET MVC project with Integrated Identity, on the first run the app will create the necessary tables in the database given in the connection string.

However, this happens once, and only once. When I delete all AspNetXXX tables and table _MigrationHistory, these tables are not re-created anymore. When started, the app throws an exception that the tables are missing. When I created a new app from the template and set its connection string to the same existing database, the new app recreated the tables for me, and the first one resumed operation properly.

My questions are:

  1. How does this project / EF know to create the ASP.NET identity tables only once and where does it store the information that this app has already created its tables?

  2. Is there a way to "reset" this for an existing project and have it re-create the tables, without activating EF Code First Migrations?

Try using the DropCreateDatabaseIfModelChanges database initializer.

It's nicely included in this tutorial: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application (search for "DropCreateDatabaseIfModelChanges" on the page).

However, consider, that you should use that only during the actual development - because if there is some difference between your model and database, whole database will be dropped and created again.

If you want to be sure, that tables are populated with some data, use Seed() method (also in the tutorial).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM