简体   繁体   English

EF7 sqlite创建表(如果不存在)

[英]EF7 sqlite create table if not exists

using c# entity framework 7 with sqlite, how can I check to see if a table exists and if not create it? 使用带有sqlite的c#实体框架7,如何检查表是否存在以及是否创建表? Maybe based off a dbset that is in the context? 也许基于上下文中的dbset? There is no existing database with a migration or anything. 没有现有数据库进行迁移或其他任何操作。 The app just creates the database when its not there and I'd like to to also create the tables. 该应用程序只是在不存在时创建数据库,我也想创建表。

public class Context : DbContext
{
    public DbSet<Value> Values { get; set; }


    protected override async void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=data.db");

        //here somewhere?
    }
}

If you are not using migrations, you can use this in your app startup procedure: 如果您不使用迁移,则可以在应用程序启动过程中使用它:

context.Database.EnsureCreated();

Or with Migrations: 或使用迁移:

context.Database.Migrate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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