首先创建Asp.net Core项目,然后通过 NuGet 安装 EntityFrameworkCore:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools (相关工具)
1、根据数据库生成Entity类:
Scaffold-DbContext "Server=ip;Database=dbName;User id=sa;Password=xxx;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
2、根据Entity类生成数据库:
Add-Migration
Update-Database -context xxx
附:生成控制器代码:
dotnet restore
dotnet tool install --global dotnet-aspnet-codegenerator --version 2.1.0
dotnet aspnet-codegenerator razorpage -m Student -dc SchoolContext -udl -outDir PagesStudents --referenceScriptLibraries
Web Api部署到IIS注意事项:
需要删除“模块”->WebDAV,才能支持其他类型的请求
附:在 SQL Server Management Studio 中删除数据库失败的处理方法:
USE master; GO ALTER DATABASE MyTestDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO ALTER DATABASE MyTestDatabase MODIFY NAME = MyTestDatabaseCopy ; GO ALTER DATABASE MyTestDatabaseCopy SET MULTI_USER GO
相关资料:
http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx (EF:实体的一对多关系如何映射成数据库表)
https://docs.microsoft.com/en-us/ef/ef6/modeling/designer/inheritance/tph (EF:按继承层次映射 & 按实体类型映射)