2

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

user2771704
  • 5,994
  • 6
  • 37
  • 38
Wakka Sakka
  • 43
  • 1
  • 5
  • Share your code for the better help.@Wakka Sakka – Rohit Poudel Aug 15 '17 at 08:38
  • Possible duplicate of [EntityFramework Core automatic migrations](https://stackoverflow.com/questions/39526595/entityframework-core-automatic-migrations) – CodeCaster Aug 15 '17 at 08:40
  • There is no extra code. I just create new aspnet core 1.1 project there is a migration folder but in server explorer is no any db. i must `update-database` manually – Wakka Sakka Aug 15 '17 at 08:47
  • ASP.NET Core never created database automatically. The template always gives scaffolded migration but it never applied. When you publish your app, it gave option to apply migrations as part of publish. If the database is not created then when running app, first time when database access is used it gave error screen with option to apply migrations. This has been behavior all along for ASP.NET Core. – Smit Aug 15 '17 at 23:56

4 Answers4

3

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.

Set
  • 47,577
  • 22
  • 132
  • 150
  • [Which breaks migrations](https://stackoverflow.com/questions/38238043/how-and-where-to-call-database-ensurecreated-and-database-migrate). – CodeCaster Aug 15 '17 at 08:42
1

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();
}
Luke
  • 22,826
  • 31
  • 110
  • 193
0

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");
        }
    }
}
Luke
  • 22,826
  • 31
  • 110
  • 193
Purple Haze
  • 530
  • 7
  • 22
  • I've got a feeling that this answer is for Asp.NET MVC and not for ASP.NET Core? I don't think ASP.NET Core has 'AutomaticMigrationsEnabled' – Luke Aug 15 '17 at 10:15
0

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.

Temi Lajumoke
  • 2,350
  • 1
  • 14
  • 14