最近两个月将12年开发的果蔬行业ERP升级到.NET Core,表结构变化比较大,迁移数据比较麻烦,所以写一个迁移程序来解决。为了减少代码量,决定用EF Core。EF Core中提供了一个命令Scaffold-DbContext,可以从数据库表生成模型类。
使用Scaffold-DbContext命令需要项目中安装 Microsoft.EntityFrameworkCore.Design 和 Microsoft.EntityFrameworkCore.SqlServer 包,如果是MySQL的话则安装 Pomelo.EntityFrameworkCore.MySql
然后就可以在 程序包管理器控制台 中运行 Scaffold-DbContext 命令了。
Scaffold-DbContext
执行 get-help Scaffold-DbContext 可以查看帮助,会输出一下信息
名称
Scaffold-DbContext
摘要
Scaffolds a DbContext and entity types for a database.
语法
Scaffold-DbContext [-Connection] <String> [-Provider] <String> [-OutputDir <String>] [-ContextDir <String>] [-Context <String>] [-Schemas <String[]>] [-Tables <String[]>] [-DataAnnotations] [-UseDatabaseNames] [-Force] [-Project <String>] [-StartupProject <String>] [<CommonParameters>]
说明
Scaffolds a DbContext and entity types for a database.
相关链接
about_EntityFrameworkCore
备注
若要查看示例,请键入: "get-help Scaffold-DbContext -examples".
有关详细信息,请键入: "get-help Scaffold-DbContext -detailed".
若要获取技术信息,请键入: "get-help Scaffold-DbContext -full".
有关在线帮助,请键入: "get-help Scaffold-DbContext -online"
参数
-Connection数据库连接字符串,必须指定,不同数据库不一样-Provider数据库的提供器-Project指定将代码生成到哪个项目下-OutputDir生成的代码放在项目的哪个目录下-Context上下文名称-Force强制重写-StartProject启动项目
Provider常用的有
- SQL Server
Microsoft.EntityFrameworkCore.SqlServer - MySQL
Pomelo.EntityFrameworkCore.MySql - SQLite
Microsoft.EntityFrameworkCore.Sqlite
示例代码
Scaffold-DbContext
-Connection "server=.;uid=user;pwd=pwd;database=databse;"
-Provider Microsoft.EntityFrameworkCore.SqlServer
-OutputDir Models
-Context MyDbConetxt
-Project xx
我程序中使用的是MySQL,Provider用的是 Pomelo.EntityFrameworkCore.MySql ,使用过程中遇到下面两个问题,同时给出解决办法。
- 主键使用的是
Guid类型,表中存的类型为Char(36),这样生成的代码中为string类型的,解决办法是在数据库连接字符串中加上GuidFormat=Char36 - 表中如果使用的是
bit类型,生成的代码中则是ulong类型的,如果想使用bool类型,则需要在数据库连接字符串中加上TreatTinyAsBoolean=false
同时要注意的是, Pomelo.EntityFrameworkCore.MySql 要使用3.0以上的版本
Scaffold-DbContext
MSSQL汇出使用套件:
Microsoft.EntityFrameworkCore.DesignMicrosoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Tools
指令(Ex):
Scaffold-DbContext "Server=localhost;Database=NICKTEST;User Id=sa;Password=xxxxx" Microsoft.EntityFrameworkCore.SqlServer -OutputDir PostgreSqlModels -Context DBContext
PostgreSQL汇出使用套件:
1.Npgsql
2.Npgsql.EntityFrameworkCore.PostgreSQL
3.Npgsql.EntityFrameworkCore.PostgreSQL.Design
指令(Ex):
Scaffold-DbContext "Host=localhost; Port=5432 ;Database=NICKTEST;Username=postgres;Password=xxxxx" Npgsql.EntityFrameworkCore.PostgreSQL -OutputDir PostgreSqlModels -Context DBContext -force
// dotnet - tool for managing a .NET source code AND BINARIES
dotnet --info -- core version installed
dotnet --help
dotnet new [template-name] --name [project-name]
dotnet new console --name MyFirstApp
dotnet add package [package]
dotnet restore
dotnet publish -c Release -o published // build and publish release version of project to specific file
// blazor boilerplate
dotnet new blazorserver -o TodoList
// from correct path
dotnet new razorcomponent -n Todo -o Pages
// dotnet util
// sc -- for communication with the Service Control Manager and services
sc create [service-name] binpath=[full-path-to-service-exe]
sc start [service-name]
sc delete [service-name]
// sc
// c# net tools
c:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\xsd xml_file
// xml to xsd
xsd invoice1.xml /outputdir:outDirXsd
xsd invoiceEu.xml /outputdir:outDirXsd
xsd invoiceGradjevina.xml /outputdir:outDirXsd
// xsd to stream class
xsd invoice1.xsd /classes /namespace:SaopSwagger
// multiple classes
xsd invoice1.xsd invoice1_app1.xsd /classes /namespace:SaopSwagger
// CORE.API
Scaffold-DbContext "Server=IT-27105\SQLEXPRESS;Database=BookStoreCodeMaze;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
// with force flag
Scaffold-DbContext -force "Server=IT-27105\SQLEXPRESS;Database=BookStoreCodeMaze;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
// schema
Scaffold-DbContext "Server=IT-27105\SQLEXPRESS;Database=AdventureWorks2016CTP3;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Schemas HumanResources -OutputDir Models
// table
Scaffold-DbContext "Server=IT-27105\SQLEXPRESS;Database=AdventureWorks2016CTP3;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Schemas HumanResources -OutputDir Models --table Artist
Scaffold-DbContext "Data Source=PromisDev;Initial Catalog=PromisDevelop;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -Schemas -OutputDir Entities -tables Drzava
Scaffold-DbContext "{ Your DB connect string }" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Scaffold-DbContext "Server=(local);Database=TestOrg;User Id=piit;password=piit123;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Scaffold-DbContext "Server=DESKTOP-UQ0555I\SQLEXPRESS;Database=BITM9;Trusted_Connection=true;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
本文介绍了如何利用EFCore的Scaffold-DbContext命令从数据库生成模型类,适用于.NETCore平台的ERP系统升级。针对MySQL数据库,需要安装Pomelo.EntityFrameworkCore.MySql,并在连接字符串中指定GuidFormat和TreatTinyAsBoolean选项来解决数据类型转换问题。同时,提供了SQLServer和PostgreSQL的示例命令。
1565

被折叠的 条评论
为什么被折叠?



