实体框架不创建数据库 [英] Entity Framework Not Creating Database

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

问题描述

一直与使用ASP.NET MVC 3项目的实体框架4.1的code首先功能玩耍。

然而,数据库(SQL Server 2008 R2)不会自动创建在应用程序启动表映射。如何使任何想法这样做?

该项目只有一个POCO:

 命名空间RIS.Models
{
    公共类Person
    {
        [键]
        公共虚拟字符串身份证{搞定;保护套; }
        公共虚拟字符串名字{获得;保护套; }
        公共虚拟字符串中间名{获得;保护套; }
        公共虚拟字符串名字{获得;保护套; }
    }
}

它还具有以下数据库上下文类:

 命名空间RIS.Models
{
    公共类RIS_DB:的DbContext
    {
        公共DbSet<&人GT;人们{搞定;组; }
    }
}

我添加了一个SQL连接字符串全局Web.config文件如下:

 <添加名称=RIS_DB的connectionString =数据源=浦原-PC;初始目录= RIS_DB;
集成安全性= SSPI;池=假的providerName =System.Data.SqlClient的/>

也有一个明确的指令来创建数据库,如果它不存在于的Global.asax.cs文件存在:

 保护无效的Application_Start()
{
    Database.SetInitializer(新CreateDatabaseIfNotExists&所述; RIS_DB>());    AreaRegistration.RegisterAllAreas();    RegisterGlobalFilters(GlobalFilters.Filters);
    的RegisterRoutes(RouteTable.Routes);
}


解决方案

打听在MSDN论坛上代替,并得到了满意的答复:

实体框架将不会创建一个数据库,直到第一次访问。在的Application_Start目前的code座()仅指定创建第一次访问期间,数据库时使用的策略。

要触发启动数据库的建立,数据库上下文的实例必须创建和 context.Database.Initialize(真)必须调用。

Been playing around with the Code First feature of Entity Framework 4.1 using an ASP.NET MVC 3 project.

However the database (SQL Server 2008 R2) does not automatically create the table mapping on application startup. Any ideas on how to make it do so?

The project has only one POCO:

namespace RIS.Models
{
    public class Person
    {
        [Key]
        public virtual string NRIC { get; protected set; }
        public virtual string FirstName { get; protected set; }
        public virtual string MiddleName { get; protected set; }
        public virtual string LastName { get; protected set; }
    }
}

It also has the following database context class:

namespace RIS.Models
{
    public class RIS_DB : DbContext
    {
        public DbSet<Person> People { get; set; }
    }
}

I've added a SQL connection string to the global web.config file as follows:

<add name="RIS_DB" connectionString="Data Source=URAHARA-PC;Initial Catalog=RIS_DB;
Integrated Security=SSPI;Pooling=False" providerName="System.Data.SqlClient"/>

There is also an explicit instruction to create the database if it does not exist in the Global.asax.cs file:

protected void Application_Start()
{
    Database.SetInitializer(new CreateDatabaseIfNotExists<RIS_DB>());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

解决方案

Asked around on the MSDN forums instead, and got a satisfactory answer:

Entity Framework will not create a database until first access. The current code block in Application_Start() only specifies the strategy to use when creating the database during first access.

To trigger creation of the database on startup, an instance of the database context must be created, and context.Database.Initialize(true) must be invoked.

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

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