Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
218 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...