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
延伸文章資訊
- 1Entity Framework Core with Existing Database
Creating entity & context classes for an existing database is called Database-First approach. EF ...
- 2[ASP.net Core] 使用Entity Framework Core Database First方式 ...
access data through Entity Framework Core Database First in separate classlibrary project. 嚴格來說,標...
- 3Database First - 資料庫優先- EF6 - Microsoft Learn
Entity Framework 6 中的資料庫優先.
- 4EF Core Database-First Tutorial for .NET Core - Devart
Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Pack...
- 5Entity Framework - Database First Approach - Tutorialspoint