Here\'s an overview of how my solution looks:
Here\'s my PizzaSoftware
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; }
}
}