Working with Database First Approach in Entity Framework Core
文章推薦指數: 80 %
So for the demo purposes I am using an class library project and an ASP.NET Core Web API project, and I am using WideWorldImporters database ...
AboutProjectsArchivesSearchFeedWorkingwithDatabaseFirstApproachinEntityFrameworkCoreJuly05,2022byAnurajEstimatedreadtime:2minsAspNetCoreEFCoreThispostisaboutworkingwithDatabaseFirstapproachinEntityFrameworkCore.ThisapproachisusefulinscenariowherewealreadygotaDatabaseandweneedtogeneratemodelanddbcontextclasses.SoforthedemopurposesIamusinganclasslibraryprojectandanASP.NETCoreWebAPIproject,andIamusingWideWorldImportersdatabasefromSQLServer.SofirstIcreatedasolutionfile,thewebapiprojectandfinallyaclasslibraryproject.AndIaddedthewebapiandclasslibraryprojectstothesolution.Andaddedthereferenceofclasslibrarytotheapiproject.Iamusing.NETCLItoolstodothis.HerearethecommandsIexecuted.dotnetnewsln
dotnetnewwebapi-oApi
dotnetnewclasslib-oData
dotnetslnadd.\Api\
dotnetslnadd.\Data\
dotnetaddreference..\Data\Data.csprojNexttoscaffoldtheentitiesanddatabasecontextweneedaddtwonugetpackages(Microsoft.EntityFrameworkCore.DesignandMicrosoft.EntityFrameworkCore.SqlServer)totheclasslibrary.dotnetaddpackageMicrosoft.EntityFrameworkCore.Design
dotnetaddpackageMicrosoft.EntityFrameworkCore.SqlServerNowwearereadytoexecutethescaffoldcommand-whichwillgeneratemodelclassesanddatabasecontext.Weareusingthedotneteftoolforscaffolding.Ifyou’renotinstalledtheEFCoretool,youneedtoinstallit.Hereisthecommandusingthedotneteftool.dotnetefdbcontextscaffold"Server=LOCALHOST;UserId=sa;Password=Password@123;Database=WideWorldImporters"Microsoft.EntityFrameworkCore.SqlServerHereisthecommandexecutionOncethecommandexecutedyouwillbeabletoseetheentitiesanddatabasecontextclassesaspartoftheclasslibraryproject.RightnowtheDatabasecontextfilecontainstheconnectionstringinsidetheOnConfiguringmethod.protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)
{
if(!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=LOCALHOST;UserId=sa;Password=Password@123;Database=WideWorldImporters");
}
}WeneedtoremovethecodesinceweareusingthislibraryintheASP.NETWebAPI.NextweneedtomodifythewebapiprojectandaddreferenceofMicrosoft.EntityFrameworkCore.SqlServer.Onceitisdone,weneedtomodifytheprogram.csfilelikethis.builder.Services
.AddDbContext
延伸文章資訊
- 1Entity Framework Core DB First 將既有資料庫轉成class | Jakeuj
結論. cd .\src\MyAPI.EntityFrameworkCore\ dotnet ef dbcontext scaffold "Server=(LocalDb)\MSSQLLocal...
- 2ASP.NET Core 3.0 如何使用Database First - ikevin 筆記本
ASP.NET Core 3.0 如何使用Database First ; Step 1. 先建立資料庫,這是參考文章提供的 ; Step 2. 從NuGet 安裝必要套件. Microsoft...
- 3Working 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,...
- 4[ASP.net Core] 使用Entity Framework Core Database First方式 ...
access data through Entity Framework Core Database First in separate classlibrary project. 嚴格來說,標...
- 5[Day05] Entity Framework Core與DB First - iT 邦幫忙- iThome
今天就來介紹ASP.NET Core 使用EF Core 來存取Mariadb(MySQL)資料庫. EF Core 有提供兩種開發方式DB First 與Code First. 本篇將以DB ...