简体   繁体   English

在App_Start中创建数据库

[英]Create database in App_Start

I have an solution with 2 projects, one domain class and one webUI. 我有一个包含2个项目,一个域类和一个webUI的解决方案。

In the domain class I have 2 models, a db context and a databas initializer. 在域类中,我有2个模型,一个数据库上下文和一个数据库初始化程序。

List.cs: List.cs:

namespace Todo.Domain
{
    public class List
    {
        public int ListID { get; set; }
        public string Day { get; set; }
        public ICollection<Task> Tasks { get; set; }
    }
}

Task.cs: Task.cs:

namespace Todo.Domain
{
    public class Task
    {
        public int TaskID { get; set; }
        public int ListID { get; set; }
        public string TodoTask { get; set; }
    }
}

EFDbContext.cs: EFDbContext.cs:

namespace Todo.Domain
{
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("TodoList") { }

        public DbSet<List> Lists { get; set; }
        public DbSet<Task> Tasks { get; set; }


    }
}

Initializer: 初始化器:

namespace Todo.Domain
{
    public class TodoDbInit : System.Data.Entity.DropCreateDatabaseIfModelChanges<EFDbContext>
    {
        protected override void Seed(EFDbContext context)
        {
            var list = new List<List>
            {
                new List { Day="Måndag" }
            };
            list.ForEach(s => context.Lists.Add(s));
            context.SaveChanges();


            var task = new List<Task>
            {
                new Task { TodoTask="Fisk" }
            };

            task.ForEach(s => context.Tasks.Add(s));
            context.SaveChanges();
        }
    }
}

Now, when I start my application, I want the database to be created. 现在,当我启动应用程序时,我希望创建数据库。 I have placed a setInitializer Global.asax: 我已经放置了一个setInitializer Global.asax:

namespace Todo.WebUI
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Database.SetInitializer(new TodoDbInit());
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

When I run my application, the database Is not created. 当我运行应用程序时,未创建数据库。 I don't know why. 我不知道为什么 Have I done something wrong? 我做错什么了吗?

You must make a call against your database in one of your controllers in order for it to be created (creation is on-demand). 您必须在一个控制器中对您的数据库进行调用才能创建它(按需创建)。 If you wish to manually create the database on application start see the answer posted here: Entity Framework code first, isn't creating the database 如果您希望在应用程序启动时手动创建数据库,请参见此处发布的答案: 首先是实体框架代码,而不是创建数据库

The following code is only setting the initializer that you wish to use against your database. 以下代码仅针对数据库设置了您希望使用的初始化程序。

Database.SetInitializer(new TodoDbInit());

In order for it to actually be used you will need to create and access entities of your db context. 为了使其能够实际使用,您将需要创建和访问数据库上下文的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 App_Start位于哪里? - Where is App_Start located? 来自NuGet的Unity Bootstrapper在App_Start上抛出错误 - Unity Bootstrapper from NuGet is throwing error on App_Start AutoFac - 在app_start上初始化重量级单身人士 - AutoFac - Initialize heavy-weight singletons on app_start 在源参数中多次指定了App_Start / Startup.Auth.cs项 - The item App_Start/Startup.Auth.cs was specified more than once in the source parameter 在App_start文件夹中专门配置ninjectwebcommon.cs文件配置Ninject - Configuring Ninject in App_start folder specifically ninjectwebcommon.cs file 如何在应用程序启动时使用 Marten 创建数据库? - How to create database with Marten on application start? 在应用程序启动之前运行数据库迁移(.NET5、EF 核心 5) - Run database migration before app start (.NET5, EF core 5) EF代码首先在使用sqlclient provider启动时创建数据库 - EF code first create database on start up with sqlclient provider 使用数据库项目创建 c# 应用程序设置 - Create c# app setup with database project 尝试为Windows Phone应用创建数据库时出现问题 - Problem trying to create database for windows phone app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM