EFCore MySql DBFirst 生成实体类

本文介绍了如何通过EFCore的scaffold-dbcontext命令从MySql数据库生成实体类和DbContext对象。首先创建EF类库并设置命名空间,接着安装必要的Nuget包。然后使用scaffold-dbcontext命令,指定数据库连接信息和目标输出目录。如果需要更新单个表,可以添加-tables参数。注意,表必须有主键才能生成实体,datetime类型字段可能需要处理。最后,可以使用-f参数覆盖现有文件,并查看命令帮助了解更多选项。
摘要由CSDN通过智能技术生成

单独生成实体类,再拷贝到

1、创建ef类库(设定一下命名空间namespace,与开发业务系统项目的一致),同时将项目设为启动项目,安装2个Nuget包,如下所示:

Microsoft.EntityFrameworkCore.Tools
Pomelo.EntityFrameworkCore.MySql

2、生成数据库的实体和ef的DbContext对象,在项目根目录下,用到的是 scaffold-dbcontext 命令;

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql"

server:数据库地址,user id:账号,password:登录密码

 如果是针对单表的更新,加一个-tables 后面是要更新的表名

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql"  -tables "mytablename"

执行完成之后会生成指定的是model ,注意:表必须有主键,才会生成,如果没有主机会报 unable to generate entity type for table “xxxx” 警告,当然实体也不会生成出现的问题:如果有表字段为 datetime类型的,生成的时候会报错 应输入标识符,处理方法:把.()去掉。

3、输出到指定文件目录(Models)下:

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql" -OutputDir Models

4、加 -f 强制覆盖文件

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql" -OutputDir Models -f

5、查看反向生成命令帮助:

dotnet ef dbcontext scaffold -h

 结果:

Usage: dotnet ef dbcontext scaffold [arguments] [options]

Arguments:
  <CONNECTION>  The connection string to the database.
  <PROVIDER>    The provider to use. (E.g. Microsoft.EntityFrameworkCore.SqlServer)

Options:
  -d|--data-annotations                  Use attributes to configure the model (where possible). If omitted, only the fluent API is used.
  -c|--context <NAME>                    The name of the DbContext. Defaults to the database name.
  --context-dir <PATH>                   The directory to put the DbContext file in. Paths are relative to the project directory.
  -f|--force                             Overwrite existing files.
  -o|--output-dir <PATH>                 The directory to put files in. Paths are relative to the project directory.
  --schema <SCHEMA_NAME>...              The schemas of tables to generate entity types for.
  -t|--table <TABLE_NAME>...             The tables to generate entity types for.
  --use-database-names                   Use table and column names directly from the database.
  --json                                 Show JSON output. Use with --prefix-output to parse programatically.
  -n|--namespace <NAMESPACE>             The namespace to use. Matches the directory by default.
  --context-namespace <NAMESPACE>        The namespace of the DbContext class. Matches the directory by default.
  --no-onconfiguring                     Don't generate DbContext.OnConfiguring.
  --no-pluralize                         Don't use the pluralizer.
  -p|--project <PROJECT>                 The project to use. Defaults to the current working directory.
  -s|--startup-project <PROJECT>         The startup project to use. Defaults to the current working directo
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值