首先是实体框架代码,不是创建数据库
以下是我的解决方案的概述:
这是我的 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当需要访问数据库时执行初始化程序。如果您想在应用程序启动时创建数据库,请使用:
或者不使用初始化程序并调用:
Initializer is executed when you need to access the database. If you want to create database on application start either use:
Or don't use initializer and call:
不要将此代码放入 main 方法中:
将其放入 DBContext 中:
Instead of putting this code into main method:
Put it into DBContext: