Entity Framework code first, isn't creating the database

送分小仙女□ 提交于 2019-11-28 09:55:06

Initializer is executed when you need to access the database. If you want to create database on application start either use:

context.Database.Initialize(true);

Or don't use initializer and call:

context.Database.CreateIfNotExists();

Instead of putting this code into main method:

Database.SetInitializer<PizzaSoftwareData>(new CreateDatabaseIfNotExists<PizzaSoftwareData>());

Put it into DBContext:

namespace PizzaSoftware.Data
{
public class PizzaSoftwareData : DbContext
{
        public PizzaSoftwareData()
        : base("name=PizzaSoftwareData")
        {
        Database.SetInitializer<PizzaSoftwareData>(new CreateDatabaseIfNotExists<PizzaSoftwareData>());
        }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<User> Users { get; set; }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!