dotnet tool install --global dotnet-ef --全局安装 ef 工具
dotnet ef migrations add init 初始化迁移
dotnet ef migrations add init -c SqlServerDbContext 初始化迁移,指定数据库上下文对象
dotnet ef migrations remove 删除迁移
dotnet ef migrations remove -c SqlServerDbContext 删除迁移,指定数据库上下文对象
dotnet ef database update 更新数据库
dotnet ef database update -c SqlServerDbContext 更新数据库,指定数据库上下文对象
dotnet ef migrations add Name 创建新的迁移文件
dotnet ef database update drop 删除数据库
Scaffold-DbContext 命令
Scaffold-DbContext 命令:生成models和context
示例:1、sqlite
Scaffold-DbContext "Data Source=EFCoreSample.db" Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models -ContextDir Context -Context DefaultDbContext
2、Mysql
Scaffold-DbContext "server=localhost;uid=root;pwd=123456;database=ShardingCoreDb" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -ContextDir ContextDir -Context MysqlDbContext
3、SqlServer
Scaffold-DbContext "Database=EFCore;Server=.,1433;User ID=sa;Password=Admin123;pooling=true;min pool size=5;max pool size=1024;connect timeout = 20;MultipleActiveResultSets=true;Encrypt=True;Trusted_Connection=True;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -ContextDir Context -Context SqlServerDbContext
指令详细介绍:
Scaffold-DbContext [-Connection] [-Provider] [-OutputDir ] [-Context ] [-Schemas ] [-Tables ] [-DataAnnotations] [ -Force] [-Project ] [-StartupProject ] [-Environment ] []
参数介绍:
-Connection 指定数据库的连接字符串。
-Provider 指定要使用的提供程序。例如,Microsoft.EntityFrameworkCore.SqlServer。
-OutputDir 指定用于输出类的目录。如果省略,则使用顶级项目目录。
-Context 指定生成的DbContext类的名称。
-Schemas 指定要为其生成类的模式。
-Tables 指定要为其生成类的表。
-DataAnnotations 使用DataAnnotation属性在可能的情况下配置模型。如果省略,输出代码将仅使用流畅的API。
-Force 强制脚手架覆盖现有文件。否则,只有在没有输出文件被覆盖的情况下,代码才会继续。
-Project 指定要使用的项目。如果省略,则使用默认项目。
-StartupProject指定要使用的启动项目。如果省略,则使用解决方案的启动项目。
-Environment 指定要使用的环境。如果省略,则使用“开发”。
-ContextDir 要在其中放置文件的目录 DbContext 。 路径相对于项目目录
-UseDatabaseNames 使用表和列的名称与数据库中显示的名称完全相同。 如果省略此参数,则更改数据库名称以更严格地符合 c # 名称样式约定。