Asp.net core ef7 遇到“no such table” 确保表格生成

64 阅读1分钟

如果你不想用那套migrate命令,你可以直接在context的构造函数里添加“Database.EnsureCreated();”,完整代码如下:

    public class UpdateItemContext : DbContext
    {
        private static bool _created = false;
        public UpdateItemContext(DbContextOptions<UpdateItemContext> options)
            : base(options)
        {
            if (!_created)
            {
                _created = true;
                Database.EnsureCreated();
            }
        }

        public DbSet<GoogleSheetUpdateItem> GoogleSheetUpdateItem { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            builder.Entity<GoogleSheetUpdateItem>() //Use your application user class here
                   .ToTable("GoogleSheetUpdateItem"); //Set the table name here
        }
    }