ASP NET MVC 3 - 如何重置数据库在代码中,有两个表和Database.Setinitializer? [英] ASP NET MVC 3 - How to reset database in code first, with two tables and Database.Setinitializer?

查看:352
本文介绍了ASP NET MVC 3 - 如何重置数据库在代码中,有两个表和Database.Setinitializer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题在于缺乏MVC经验。基本上,我在DB
-Person
- 提供两个表

My problem lies in the lack of experience in MVC. Basically, I have two tables in DB -Person -Offer

对于每一个我已经创建了一个模型和一个控制器和一个模型,因此结构看起来像:

For each I have created a model and a controller and a model, so the structure looks like that:

public class Offer
{
    public int OfferID { get; set; }
    public string OfferTitle { get; set; }
    public string State { get; set; }
    public string Description { get; set; }
}

public class OfferDBContext : DbContext
{
    public DbSet<Offer> Offers { get; set; }
}

这是收购的模式。

public class Person
{
    public int PersonID { get; set; }
    public string Username { get; set; }
    public DateTime Birthday { get; set; }
    public string Education { get; set; }
    public string Email { get; set; }
}

public class PersonDBContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
}

这是人的模式。

首先,我创建的Person模式,即添加本身没有任何问题分贝。然后,我想补充报价表,我不得不使用DropCreateDatabaseIfModelChanges方法。我用了OfferInitializer和PersonInitializer再有就是Global.asax.cs中文件

Firstly I created the Person model, that added itself to db without any problems. Then I wanted to add Offer table, and I had to use the DropCreateDatabaseIfModelChanges method. I used it for OfferInitializer and PersonInitializer and then there is the Global.asax.cs file

protected void Application_Start()
{
    Database.SetInitializer<OfferDBContext>(new OfferInitializer());
    Database.SetInitializer<PersonDBContext>(new PersonInitializer());

    //Database.SetInitializer<PersonDBContext>(new PersonInitializer());

    AreaRegistration.RegisterAllAreas();

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



据我了解,我不能这样做,仅仅是因为我删除数据库2次,每次在一个时间填充只有一个表。我如何重新组织这一切,让我可以一次填充两个或多个表,或整个数据库?

From what I understand, I cant do that simply because I am dropping database 2 times, each time populating only one table at a time. How do I reorganize it all, so that I can populate both or more tables at a time, or the whole database?

推荐答案

首先第一件事情,你不应该为每个表创建单独的的DbContext 类。您应该把所有的 DbSets 在同一个的DbContext 。这样做将简化事情极大地为您服务。

First things first, you should not create individual DbContext classes for each table. You should instead put all your DbSets in the same DbContext. Doing this will simplify things greatly for you.

其次,你应该考虑使用迁移。你应该开始在项目的早期使用它们。

Secondly, you should look into using migrations. You should start using them very early in your project.

您使用的软件包管理控制台代码先迁移工作。

You work with code first migrations using the Package Management Console.

enable-migrations

不正是顾名思义。在初始化项目迁移。这将创建您的项目中的文件夹,并生成所需的文件。

Does exactly what the name implies. Initializes migrations in your project. This will create a folder inside your project and generate the files needed.

add-migration InitialCreate

这将创建一个迁移。 InitialCreate实际上是一个串,你可以把它改成任何你想要的。该命令将生成创建从strach数据库所需的脚本。

This creates a migration. InitialCreate is actually a string and you can change it to whatever you want. This command will generate the scripts needed to create the database from strach.

update-database

该命令将验证数据库和应用迁移(或迁移 - 可以有多个),以获得数据库所需达到最新

This command verifies the database and applies the migration (or migrations - there can be multiple) required in order to get the database up-to-date.

这是初始设置。如果你做进一步的更改您的第一个代码的第一课,或添加更多,你将只需要添加一个新的迁移,然后执行它。

This is the initial setup. If you do further changes to your first code first classes, or add more, you will just have to add a new migration and then execute it.

add-migration AddedFirstName
update-database

就这么简单!

有像种子,回滚,更新特定迁移等一些更高级的概念,但是上面我所输入涵盖了基础知识和当日迁移日常使用。

There are some more advanced concepts like seed, rollback, update to specific migration, etc., but what I have typed above covers the basics and the day to day usage of migrations.

我建议你阅读这篇文章,其中解释了更详细的一切:的 HTTP:/ /www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc - 应用

I recommend you to read this article which explains everything in much more detail: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

这篇关于ASP NET MVC 3 - 如何重置数据库在代码中,有两个表和Database.Setinitializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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