我们正在将 ASP.NET MVC 4.x 应用程序移植到 ASP.NET Core 3.1.当前应用程序使用 EF 6.x DB 第一种方法.作为此次迁移的一部分,我们将使用 EF Core 3.1 作为当前 EF 6.x 的替代方案.所以问题是:
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:
EF Core 3.1 是否支持 DB First 方法?
如果没有,有什么选择?我们是否只剩下代码优先的方法?
If not, what are the options? Are we left with only code first approach?
感谢您的帮助.
推荐答案是的.从 .NET Core 1.0 到现在,它都支持 DB First Approach.你需要从nugets下载4个
Yes. It supports DB First Approach since .NET Core 1.0 until now. You need to download 4 from nugets
EntityFrameworkCore
EntityFrameworkCore
EntityFrameworkCore.Design
EntityFrameworkCore.Design
EntityFrameworkCore.Tools
EntityFrameworkCore.Tools
EntityFrameworkCore.SqlServer
EntityFrameworkCore.SqlServer
打开工具>NuGet 包管理器包管理器控制台.并在控制台中输入以下内容.
Open Tools > NuGet Package Manager > Package Manager Console. And enter this below in console.
默认:
Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext"看到您对Scaffold-DbContext 仅创建代码优先模型"的评论.不,Scaffold-DbContext 是数据库优先的方法.
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."
已编辑
如果你在数据库中有新的更新并且想要更新 dbcontext,只需在末尾添加 -f.它将更新并覆盖您所有的 dbcontext 和模型类.
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如果要在model类中添加一些数据注解如[Column]等,可以添加-DataAnnotations
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更多推荐
EF Core 3.1 是否支持 DB First 方法?
发布评论