使用数据库优先方法时如何更新模型

我首先使用 EntityFrameworkCore 数据库来创建模型 如 EF Core 文档所示

但我不知道如何更新模型时,数据库已被编辑。

119372 次浏览

You can re-scaffold the model by running the command that you originally ran with the -Force option added. That will result in the contents of the specified folder being over-written. Using the Package Manager Console example from the EF Core docs, the revised command becomes:

Scaffold-DbContext "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

Alternatively, if you are using CLI commands, it becomes:

dotnet ef dbcontext scaffold "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f

However, you should consider using Migrations to keep your model and database schema in sync with each other. That way you make changes to the model and then propagate them to the database.

Open your ContextModel.edmx file to display the model diagram. Right-click anywhere on the design surface, and select Update Model from Database... In the Update Wizard, select the Refresh tab and select your table then click Finish button.

For more details with picture visit: EF Database First with ASP.NET MVC: Changing the Database

Additional Tip

If you are going to update the models from time to time, here's a convenient way to simplify the process.

Head over to menu Tools > External Tools, and then Add a new menu and fill in the following entries:


Title:

Update DbContext

Command:

dotnet.exe

Arguments:

ef dbcontext scaffold "your-connection-string" Microsoft.EntityFrameworkCore.SqlServer --output-dir=Models --force

Initial directory:

$(ProjectDir)

Then optionally tick "Use Output window", hit Apply and OK.

When you go to Tools again, this new menu should be there and ready for reuse, in just a click of a button!

If we have customize in dbcontext class E.g. add LoggerFactory and then after we are using ('Scaffold-DbContext "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force'). this command then all customize changes will be lost.

You need to do a migration DO NOT rescaffold or you will lose any work done on the models, such as data validations.

Use command Add-migration NameOfMigrationfile which create in migration folder at the application level.

If auto migration is not enabled then we can use some below commands in Package Manager Console.

PM > Enable-migrations -force (If automigration not enable) PM > Add-migration MigrationName PM > Update-database -force (If add-migration command not working then we can use udate command)

Solution given by Mike worked for me with no problem. A bit of elaborated answer is here based on the answer of Mike.

Scaffold-DbContext "Server=\{\{Server name}};Database=\{\{Database name}};User ID=\{\{Login}};password=\{\{Password}}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir \{\{Folder name}} -Force
  • Folder name : Folder name under the project. The folder must have to created before executing this command.

Sample

Scaffold-DbContext "Server=.;Database=EmployeeDB;User ID=sa;password=12345" Microsoft.EntityFrameworkCore.SqlServer -OutputDir "./EmployeeDBModel" -Force

In case you are not using SQL Server authentication please refer to the answer given by Mike.

You can use this extension: EF Core Power Tools , it will make your life much easier, and you will not have to write any command line.

For the ones that prefer to keep all in the same DbContext class, use Scaffold-DbContext with option -Context.

example:

Scaffold-DbContext "Server=server;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -Context MyDbContext

The generated code for the MyDbContext will be placed on a new partial class file, so no code will be lost.

you may

  1. rename folder with scaffolded clasess let's say model->model1
  2. scaffold database with original command
  3. delete model1 folder

but if you have any changes in /model folder you will lose them. i am keeping /Model folder nearly intact and have all changes in /Partial folder (partial classes).

the only thing i need to change manually after rescaffolding isto remove constructor and onconfiguring (etc) from newly created dbcontext class, because i am keeping this code in partial class