Here's an overview of how my solution looks:
Here's my PizzaSoftwareData class:
namespace PizzaSoftware.Data
{
public class PizzaSoftwareData : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
}
}
According to an example on Scott Guthrie's blog, you have to run this code at the beginning of the application in order to create/update the database schema.
Database.SetInitializer<PizzaSoftwareData>(new CreateDatabaseIfNotExists<PizzaSoftwareData>());
I'm running that line of code from Program.cs in PizzaSoftware.UI.
namespace PizzaSoftware.UI
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Database.SetInitializer<PizzaSoftwareData>(new CreateDatabaseIfNotExists<PizzaSoftwareData>());
Application.Run(new LoginForm());
}
}
}
Can anyone tell me why the database isn't having the tables created?
Here's the connection string in my App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="PizzaSoftwareData"
connectionString="Data Source=.SQLEXPRESS;Initial Catalog=SaharaPizza;Integrated Security=True;Pooling=False"
providerName="System.Data.Sql" />
</connectionStrings>
</configuration>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…