EF Core Database-First Tutorial for .NET Core - Devart
文章推薦指數: 80 %
Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and ...
dotConnectforPostgreSQLDocumentation
EntityFrameworkCoreDatabase-FirstTutorialfor.NETCore
[email protected]
EntityFrameworkCoresupportsDatabase-FirstapproachviatheScaffold-DbContextcommandofPackageManagerConsole.ThiscommandscaffoldsaDbContextandentitytypeclassesforaspecifieddatabase.
Thistutorialshowshowtocreateasimpleconsoleapplication,
poweredbyEntityFrameworkCoreandusingDatabase-Firstapproach.Inlessthan10minutesyouwillhavea
ready-to-usedataaccesslayerforyourbusinessobjects.
Thistutorialisfor.NETCore.ForFull.NETFrameworkseeEntityFrameworkCoreDatabase-FirstTutorialforFull.NETFramework.
Requirements
Thistutorialrequiresthefollowing:
VisualStudio2017orhigher(forEntityFrameworkCore3.1-VisualStudio2019)
IfyouwanttouseEntityFrameworkCore2.2-.NETCoreSDK2.2.ForEntityFrameworkCore3.1-.NETCoreSDK3.0
LatestversionofNuGetPackageManager
LatestversionofWindowsPowerShell
Wewillusethetables,wehavecreatedintheCreatingDatabaseObjectsandInsertingDataIntoTablestutorials.Inthistutorialitisassumedthatyoualreadyhavethedatabaseobjectscreated.Ifyouhaven'tcreatedthemyet,pleasedoitbeforestartingthistutorial.
CreatingNewProject
OpenVisualStudio
OntheFilemenupointtoNewandthenclickProject.TheNewProjectdialogboxwillopen.
OntheleftsideofthedialogboxselectInstalled->VisualC#->.NETCore.
OntherightsideofthedialogboxselectConsoleApp.
Entertheprojectnameandlocationifnecessary.
ClickOK.Anewprojectwillbecreated.
InstallingNuGetPackages
Afterwecreatedanewproject,weneedtoaddthenecessaryNuGetpackagestoit.
Nowlet'sinstallthenecessarypackages:
OntheToolsmenupointtoNuGetPackageManagerandthenclickPackageManagerConsole.
ForEntityFrameworkCore5,runthefollowingcommandinthePackageManagerConsole:
Install-PackageMicrosoft.EntityFrameworkCore.Tools
ForEntityFrameworkCore3.1,runthefollowingcommandinthePackageManagerConsole:
Install-PackageMicrosoft.EntityFrameworkCore.Tools-Version3.1.10
ForEntityFrameworkCore2.2,runthefollowingcommandinthePackageManagerConsole:
Install-PackageMicrosoft.EntityFrameworkCore.Tools-Version2.2.6
ForEntityFrameworkCore1.1,thecommandwillbethefollowing:
Install-PackageMicrosoft.EntityFrameworkCore.Tools-Version1.1.5
ForEntityFrameworkCore3.1or5.0runthefollowingcommandinthePackageManagerConsole:
Install-PackageDevart.Data.PostgreSql.EFCore
ForEntityFrameworkCore1.1,thecommandwillbethefollowing:
Install-PackageDevart.Data.PostgreSql.EFCore.Design
NotethatthedotConnectforPostgreSQLassemblyforEntityFrameworkCore2.2isnotavailableviaNuGetpackagesanymore.ItisinstalledbythedotConnectforPostgreSQLinstallertothe\Entity\EFCore2\netstandard2.0\subfolderofthedotConnectforPostgreSQLinstallationfolder.
CreatingaModelFromtheDatabase
ForEntityFrameworkCore,creatingamodelfromthedatabaseisaseasyasenteringtheScaffold-DbContextcommandwithaconnectionstringandaproviderasparameters.Forexample,youcanrunthefollowingcommandinthePackageManagerConsole:
Scaffold-DbContext"host=server;database=test;userid=postgres;"Devart.Data.PostgreSql.Entity.EFCore
Ifyouhaveothertablesinyourdatabase,youmayuseadditionalparameters--Schemasand-Tables-tofilterthelistofschemasand/ortablesthatareaddedtothemodel.Forexample,youcanusethefollowingcommand:
Scaffold-DbContext"host=server;database=test;userid=postgres;"Devart.Data.PostgreSql.Entity.EFCore-Tablesdept,emp
Astheresult,acontextclassandentityclassesaregenerated,basedontheDeptandEmptables.
C#csharpCopyCode
usingSystem;
usingSystem.Collections.Generic;
namespaceConsoleApplication1
{
publicpartialclassDept
{
publicDept()
{
Emp=newHashSet
延伸文章資訊
- 1Database First - 資料庫優先- EF6 - Microsoft Learn
Entity Framework 6 中的資料庫優先.
- 2Working with Database First Approach in Entity Framework Core
So for the demo purposes I am using an class library project and an ASP.NET Core Web API project,...
- 3ASP.NET Core 3.0 如何使用Database First - ikevin 筆記本
ASP.NET Core 3.0 如何使用Database First ; Step 1. 先建立資料庫,這是參考文章提供的 ; Step 2. 從NuGet 安裝必要套件. Microsoft...
- 4Database First - EF6 | Microsoft Learn
- 5EF Core Database-First Tutorial for .NET Core - Devart