Entity Framework code first, isn't creating the database

后端 未结 2 688
旧时难觅i
旧时难觅i 2020-12-10 03:51

Here\'s an overview of how my solution looks:

\"enter

Here\'s my PizzaSoftware

相关标签:
2条回答
  • 2020-12-10 04:23

    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();
    
    0 讨论(0)
  • 2020-12-10 04:26

    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; }
        }
    }
    
    0 讨论(0)
提交回复
热议问题