实体框架代码首先,是不是在创建数据库 [英] Entity Framework code first, isn't creating the database

查看:141
本文介绍了实体框架代码首先,是不是在创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的解决方案是如何看的概述:





下面是我的PizzaSoftwareData类:

 命名空间PizzaSoftware.Data 
{
公共类PizzaSoftwareData:的DbContext
{
公共DbSet<客户>客户{搞定;组; }
公共DbSet<排序>订单{搞定;组; }
公共DbSet<产品与GT;产品{搞定;组; }
公共DbSet<使用者>用户{搞定;组; }
}
}



据斯科特Guthrie的博客为例,你。要以创建/更新数据库架构运行在应用程序的开头这段代码

  Database.SetInitializer< PizzaSoftwareData> (新CreateDatabaseIfNotExists&所述; PizzaSoftwareData>()); 



我跑这条线从Program.cs中的代码PizzaSoftware.UI。



 命名空间PizzaSoftware.UI 
{
静态类节目
{
///<总结>
///的主入口点应用程序。
///< /总结>
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Database.SetInitializer&所述; PizzaSoftwareData>(新CreateDatabaseIfNotExists&所述; PizzaSoftwareData>());
Application.Run(新LoginForm的());
}
}
}



谁能告诉我为什么数据库不具有表创建?



下面是我App.config文件的连接字符串:

  < XML版本=1.0编码=UTF-8>?; 
<结构>
<&是connectionStrings GT;
<添加名称=PizzaSoftwareData
的connectionString =数据源= .\SQLEXPRESS;初始目录= SaharaPizza;集成安全性= TRUE;池=假
的providerName =系统。 Data.Sql/>
< /&是connectionStrings GT;
< /结构>


解决方案

当你需要访问数据库的初始化程序被执行。如果你想创建应用程序数据库启动或者使用:

  context.Database.Initialize(真); 



或者不使用初始化,并呼吁:

  context.Database.CreateIfNotExists(); 


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>

解决方案

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

这篇关于实体框架代码首先,是不是在创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆