首先是实体框架代码,不是创建数据库

发布于 2024-10-26 04:01:38 字数 1705 浏览 0 评论 0原文

以下是我的解决方案的概述:

在此处输入图像描述

这是我的 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-11-02 04:01:38

当需要访问数据库时执行初始化程序。如果您想在应用程序启动时创建数据库,请使用:

context.Database.Initialize(true);

或者不使用初始化程序并调用:

context.Database.CreateIfNotExists();

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();
凉月流沐 2024-11-02 04:01:38

不要将此代码放入 main 方法中:

Database.SetInitializer<PizzaSoftwareData>(new CreateDatabaseIfNotExists<PizzaSoftwareData>());

将其放入 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; }
    }
}

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; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文