以下是我的解决方案的概述:
这是我的PizzaSoftwareData类:
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; } } }
根据Scott Guthrie博客的一个例子,您必须在应用程序开始时运行此代码才能创建/更新数据库模式.
Database.SetInitializer<PizzaSoftwareData>(new CreateDatabaseIfNotExists<PizzaSoftwareData>());
我正在PizzaSoftware.UI中的Program.cs中运行一行代码.
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()); } } }
任何人都可以告诉我为什么数据库没有创建表?
以下是我的App.config文件中的连接字符串:
<?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>