auto create database in Entity Framework Core

前端 未结 5 745
南笙
南笙 2020-11-29 17:43

My application which is being ported to .NET core will use the new EF Core with SQLite. I want to automatically create the database and table structures when the app is firs

5条回答
  •  失恋的感觉
    2020-11-29 18:28

    If you haven't created migrations, there are 2 options

    1.create the database and tables from application Main:

    var context = services.GetRequiredService();
    context.Database.EnsureCreated();
    

    2.create the tables if the database already exists:

    var context = services.GetRequiredService();
    context.Database.EnsureCreated();
    RelationalDatabaseCreator databaseCreator =
    (RelationalDatabaseCreator)context.Database.GetService();
    databaseCreator.CreateTables();
    

    Thanks to Bubi's answer

提交回复
热议问题