EF Core 3.1 是否支持 DB First 方法? [英] Does EF Core 3.1 support DB First approach?

查看:23
本文介绍了EF Core 3.1 是否支持 DB First 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将 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

  1. EntityFrameworkCore

  1. 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 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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