如何结合代码优先和数据库优先方法 [英] How to combine both of code first and database first approaches

查看:150
本文介绍了如何结合代码优先和数据库优先方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我是一家公司的新开发人员。因此,该项目已经有一个现有的数据库。要进行该项目,显然我需要使用现有数据库(数据库优先方法),该数据库可以生成模型类供我处理。

Let's say I'm a new developer in a company. So there is already an existing database for the project. To work on the project, obviously I need to scaffold the existing database(database first approach), which can generate model classes for me to work on.

所以我开始在项目上工作,并希望向表中添加新列,因此我在模型类上添加了新属性,然后将这一更改应用到数据库中。因此,我通过添加新的迁移并更新数据库来切换回代码优先方法。

So I start to work on the project and want to add a new column to a table, so I add a new property on the model class then I want to apply this change in the database. So I switch back to code first approach by adding a new migration and update the database.

但是问题是,如果我添加新的迁移,EF将包括所有迁移的UP方法中使用数据模型类来创建数据库中的所有对应表,因为EF认为所有模型类都是我新添加的。那么如何只通过添加新列而不更新其他表来更新表?

But the problem is, if I add a new migration, EF will include all the data model classes in the migration's UP method to create all the corresponding tables in the database because EF think all the model classes are newly added by me. So how can I only get the table updated by adding a new column without re-create all other tables?

推荐答案

1)反向从现有数据库中设计模型和数据库上下文。为此,请点击此链接, https:// docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

1) Reverse engineer the models and db context from the existing database. Follow this link for that, https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

2)现在,您有了模型并db上下文。

2) Now you have the models and db context. Your data access should work fine with the new context, except migrations.

3)既然已经创建了数据库和表,则需要调整数据库以假装

3) Now that you already have the database and tables created, you need to tweak your DB to pretend that current schema is created using migrations.

3.1)首先创建初始迁移 Add-Migration IntitialCreate

3.1) First create the Initial Migration Add-Migration IntitialCreate. It will have code to create the database up to current level.

3.2)现在,您必须手动创建__MigrationHistory表,并将IntialMigration手动插入该表中,以便它不会尝试再次应用它。如果您发现很难手动执行此操作,则可以使用技巧。

3.2) Now you should have to manually create __MigrationHistory table and manually insert IntialMigration to the table, so that it wont try to apply it again. If you find hard to do this manually, there is trick.

使用当前的迁移创建新数据库,只需更改连接字符串中的数据库名称并执行 update-database 。在新创建的数据库中,应在已应用InitialCreate迁移的情况下创建__MigrationHistory表。生成一个创建脚本以及数据并在原始数据库中执行。

Create a new database using you current migrations by just changing the Database name in the connection string and executing update-database. In the new database created __MigrationHistory table should be created with InitialCreate migration already applied. Generate a create script along with data and execute that in the original database.

4)现在,您可以对模型进行其余的更改,创建新的迁移应用它们像往常一样。

4) Now you can do the rest of the changes to the model, create new migrations apply them as usual.

这篇关于如何结合代码优先和数据库优先方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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