Working with Database First Approach in Entity Framework Core

文章推薦指數: 80 %
投票人數:10人

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(options=>options.UseSqlServer(builder.Configuration!.GetConnectionString("WideWorldImportersContext")!));NowwecanaccessandusetheWideWorldImportersContextinthecontrollerconstructor.HappyProgramming:)AboutAnurajisaMicrosoftMVP,AzureArchitect,TechnicalEvangelist,TechnologyExpert,MentorandProductArchitectofSocxoandSocxly.FollowhimonTwitterandGithubArchivesOctober2022(1)September2022(3)August2022(5)July2022(4)June2022(4)May2022(5)April2022(4)March2022(3)February2022(4)January2022(1)SubscribeSubscribeRecentPostsAddUnitTestsToYourAzureFunctionsBuildingASP.NETCoreMinimalAPIinVB.NETImplementingGoogleWebRiskAPIinASPNETCoreStreamingVideosfromAzureBlobStorageCustomModelBindinginASP.NETCoreMinimalAPIs



請為這篇文章評分?