Does EF Core 3.1 support DB First approach?

混江龙づ霸主 提交于 2021-02-16 04:01:10

问题


We are porting an ASP.NET MVC 4.x application to ASP.NET Core 3.1. The current application is using EF 6.x DB first approach. As a part of this migration we are going to use EF Core 3.1 as an alternative to the current EF 6.x. So the question is:

Does EF Core 3.1 support DB First approach?

If not, what are the options? Are we left with only code first approach?

Appreciate your helps.


回答1:


Yes. It supports DB First Approach since .NET Core 1.0 until now. You need to download 4 from nugets

  1. EntityFrameworkCore

  2. EntityFrameworkCore.Design

  3. EntityFrameworkCore.Tools

  4. EntityFrameworkCore.SqlServer

Open Tools > NuGet Package Manager > Package Manager Console. And enter this below in console.

Default:

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext"

Saw your comment about "Scaffold-DbContext only creates a Code First model". No, Scaffold-DbContext is Database-First approach.

"Creating entity & context classes for an existing database is called Database-First approach."

EDITED

If you have new update in database and want to update dbcontext, just add -f at end. It will update and overwrite all your dbcontext and model classes.

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext" -f

If you want to add some data annotations such as [Column], etc in model class, can add -DataAnnotations

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext" -DataAnnotations -f



回答2:


Yes, EF Core supports database first via the Scaffold-DbContext command, and you can also use EF Core Power Tools. Edmx based modelling is not available with EF Core, only code based modelling.




回答3:


No, unfortunately there is no DB first approach anymore.

What you can do is code first from existing database. With the scaffold command that was already mentioned.

What we do:

  • Use scaffold
  • Create "scaffold scripts" that control the scaffold process. (For example split the tables into more than one context)
  • Create "after scaffold scripts" that repair all mistakes scaffold makes every time. (for example broken data types)
  • Create more "after scaffold scripts" that enhance your models with all scaffold does not support. (for example column comments)
  • Use partial classes as much as possible to avoid losing changes every time

But in summary there is no satisfactory solution for that. Be aware that if you want a perfect db first approach you almost rewrite the whole scaffold functionality.



来源:https://stackoverflow.com/questions/59810607/does-ef-core-3-1-support-db-first-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!