Wakka Sakka
Wakka Sakka

Reputation: 53

ASP.NET Core new project not creating database by default

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

Upvotes: 3

Views: 4854

Answers (4)

Temi Lajumoke
Temi Lajumoke

Reputation: 2400

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.

Upvotes: 0

Purple Haze
Purple Haze

Reputation: 550

To enable automatic migrations you need to add this to your configuration :

public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DbContext>
{
public MigrateDBConfiguration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}
}

To enable automatic updates to your database you need to add Database.SetInitializer(...) in OnModelCreating() method on your context :

public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MigrateDBConfiguration>());
}

...
}

You can also use Fluent migration to make the database updates and migrations automatic.
Here's an example on how to use it from Here:

using FluentMigrator;
namespace DatabaseMigration.Migrations
{
    [Migration(1)]
    public class M0001_CreateMemberTable:Migration
    {
        public override void Up()
        {
            Create.Table("Member")
                .WithColumn("MemberId").AsInt32().PrimaryKey().Identity()
                .WithColumn("Name").AsString(50)
                .WithColumn("Address").AsString()
                .WithColumn("MobileNo").AsString(10);
        }

        public override void Down()
        {
            Delete.Table("Member");
        }
    }
}

Upvotes: 0

Set
Set

Reputation: 49779

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.

Upvotes: 3

Luke
Luke

Reputation: 23690

You have to trigger it to automatically migrate your database. Unlike previous versions of ASP.NET MVC, this isn't implemented out of the box.

A way that this can be achieved, however, is to trigger it from your startup.cs, or somewhere else early in the pipeline like so:

using (var context = new MyDbContext(..))
{
    context.Database.Migrate();
}

Upvotes: 1

Related Questions