以下是我的解决方案的概述:
这是我的PizzaSoftwareData类:
namespace PizzaSoftware.Data
{
public class PizzaSoftwareData : DbContext
{
public DbSet Customers { get; set; }
public DbSet Orders { get; set; }
public DbSet Products { get; set; }
public DbSet Users { get; set; }
}
}
根据Scott Guthrie博客的一个例子,您必须在应用程序开始时运行此代码才能创建/更新数据库模式.
Database.SetInitializer(new CreateDatabaseIfNotExists());
我正在PizzaSoftware.UI中的Program.cs中运行一行代码.
namespace PizzaSoftware.UI
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Database.SetInitializer(new CreateDatabaseIfNotExists());
Application.Run(new LoginForm());
}
}
}
任何人都可以告诉我为什么数据库没有创建表?
以下是我的App.config文件中的连接字符串:
connectionString="Data Source=.\sqlEXPRESS;Initial Catalog=SaharaPizza;Integrated Security=True;Pooling=False"
providerName="System.Data.sql" />