ASP.NET Core new project not creating database by default

asp.net-core asp.net-mvc entity-framework-core visual-studio-2017

Question

I created new ASP.NET Core project with Identity Individual User but it did not create any database.

I have to use add-migration update-database manually.

I remember, in the past, everything was done automatically. Dunno what's wrong this time.

VS2017.3

1
2
8/15/2017 10:57:18 AM

Accepted Answer

Depending on the version of dotnet core you have, ASP.NET core may not automatically create a database for you when you create a new app.

However, simply migrating and updating the database should work. First, create a new migration with the syntax dotnet ef migrations add <MIGRATION_NAME>. Like so

dotnet ef migrations add InitialMigration

and then you update the database like so

dotnet ef database update .

This should do the trick.

0
8/15/2017 1:16:31 PM

Popular Answer

If you want to create a database schema only, you may call:

//IDbContext context;
context.Database.EnsureCreated();

But note, that

EnsureCreated totally bypasses migrations and just creates the schema for you, you can't mix this with migrations. EnsureCreated is designed for testing or rapid prototyping where you are ok with dropping and re-creating the database each time. If you are using migrations and want to have them automatically applied on app start, then you can use context.Database.Migrate() instead.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow