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

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

问题描述

我们正在将 ASP.NET MVC 4.x 应用程序移植到 ASP.NET Core 3.1 .当前应用程序正在使用 EF 6.x DB first方法.作为此迁移的一部分,我们将使用 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.您需要从nuget下载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

如果要在模型类中添加一些数据注释,例如[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天全站免登陆