ASP.NET Web Deployment using Visual Studio - Microsoft Learn

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

Deploy the database update · Open the Publish Web wizard by right-clicking the ContosoUniversity project and clicking Publish. · Select the Test ... Skiptomaincontent Thisbrowserisnolongersupported. UpgradetoMicrosoftEdgetotakeadvantageofthelatestfeatures,securityupdates,andtechnicalsupport. DownloadMicrosoftEdge MoreinfoaboutInternetExplorerandMicrosoftEdge Tableofcontents Exitfocusmode ReadinEnglish Save Tableofcontents ReadinEnglish Save Edit Print Twitter LinkedIn Facebook Email Tableofcontents ASP.NETWebDeploymentusingVisualStudio:DeployingaDatabaseUpdate Article 07/21/2022 8minutestoread 8contributors Inthisarticle byTomDykstra DownloadStarterProject Thistutorialseriesshowsyouhowtodeploy(publish)anASP.NETwebapplicationtoAzureAppServiceWebAppsortoathird-partyhostingprovider,byusingVisualStudio2012orVisualStudio2010.Forinformationabouttheseries,seethefirsttutorialintheseries. Overview Inthistutorial,youmakeadatabasechangeandrelatedcodechanges,testthechangesinVisualStudio,thendeploytheupdatetothetest,staging,andproductionenvironments. ThetutorialfirstshowshowtoupdateadatabasethatismanagedbyCodeFirstMigrations,andthenlateritshowshowtoupdateadatabasebyusingthedbDacFxprovider. Reminder:Ifyougetanerrormessageorsomethingdoesn'tworkasyougothroughthetutorial,besuretocheckthetroubleshootingpage. DeployadatabaseupdatebyusingCodeFirstMigrations Inthissection,youaddabirthdatecolumntothePersonbaseclassfortheStudentandInstructorentities.Thenyouupdatethepagethatdisplaysinstructordatasothatitdisplaysthenewcolumn.Finally,youdeploythechangestotest,staging,andproduction. Addacolumntoatableintheapplicationdatabase IntheContosoUniversity.DALproject,openPerson.csandaddthefollowingpropertyattheendofthePersonclass(thereshouldbetwoclosingcurlybracesfollowingit): [DisplayFormat(DataFormatString="{0:d}",ApplyFormatInEditMode=true)] [Display(Name="BirthDate")] publicDateTime?BirthDate{get;set;} Next,updatetheSeedmethodsothatitprovidesavalueforthenewcolumn.OpenMigrations\Configuration.csandreplacethecodeblockthatbeginsvarinstructors=newListwiththefollowingcodeblockwhichincludesbirthdateinformation: varinstructors=newList { newInstructor{FirstMidName="Kim",LastName="Abercrombie",HireDate=DateTime.Parse("1995-03-11"),BirthDate=DateTime.Parse("1918-08-12"),OfficeAssignment=newOfficeAssignment{Location="Smith17"}}, newInstructor{FirstMidName="Fadi",LastName="Fakhouri",HireDate=DateTime.Parse("2002-07-06"),BirthDate=DateTime.Parse("1960-03-15"),OfficeAssignment=newOfficeAssignment{Location="Gowan27"}}, newInstructor{FirstMidName="Roger",LastName="Harui",HireDate=DateTime.Parse("1998-07-01"),BirthDate=DateTime.Parse("1970-01-11"),OfficeAssignment=newOfficeAssignment{Location="Thompson304"}}, newInstructor{FirstMidName="Candace",LastName="Kapoor",HireDate=DateTime.Parse("2001-01-15"),BirthDate=DateTime.Parse("1975-04-11")}, newInstructor{FirstMidName="Roger",LastName="Zheng",HireDate=DateTime.Parse("2004-02-12"),BirthDate=DateTime.Parse("1957-10-12")} }; Buildthesolution,andthenopenthePackageManagerConsolewindow.MakesurethatContosoUniversity.DALisstillselectedastheDefaultproject. InthePackageManagerConsolewindow,selectContosoUniversity.DALastheDefaultproject,andthenenterthefollowingcommand: add-migrationAddBirthDate Whenthiscommandfinishes,VisualStudioopenstheclassfilethatdefinesthenewDbMigrationclass,andintheUpmethodyoucanseethecodethatcreatesthenewcolumn.TheUpmethodcreatesthecolumnwhenyouareimplementingthechange,andtheDownmethoddeletesthecolumnwhenyouarerollingbackthechange. Buildthesolution,andthenenterthefollowingcommandinthePackageManagerConsolewindow(makesuretheContosoUniversity.DALprojectisstillselected): update-database TheEntityFrameworkrunstheUpmethodandthenrunstheSeedmethod. DisplaythenewcolumnintheInstructorspage IntheContosoUniversityproject,openInstructors.aspxandaddanewtemplatefieldtodisplaythebirthdate.Additbetweentheonesforhiredateandofficeassignment: '> 'Width="7em"> '> ' Width="7em"> '> (Ifcodeindentationgetsoutofsync,youcanpressCTRL-KandthenCTRL-Dtoautomaticallyreformatthefile.) RuntheapplicationandclicktheInstructorslink. Whenthepageloads,youseethatithasthenewbirthdatefield. Closethebrowser. Deploythedatabaseupdate InSolutionExplorerselecttheContosoUniversityproject. IntheWebOneClickPublishtoolbar,clicktheTestpublishprofile,andthenclickPublishWeb.(Ifthetoolbarisdisabled,selecttheContosoUniversityprojectinSolutionExplorer.) VisualStudiodeploystheupdatedapplication,andthebrowseropenstothehomepage. RuntheInstructorspagetoverifythattheupdatewassuccessfullydeployed. Whentheapplicationtriestoaccessthedatabaseforthispage,CodeFirstupdatesthedatabaseschemaandrunstheSeedmethod.Whenthepagedisplays,youseetheexpectedBirthDatecolumnwithdatesinit. IntheWebOneClickPublishtoolbar,clicktheStagingpublishprofile,andthenclickPublishWeb. RuntheInstructorspageinstagingtoverifythattheupdatewassuccessfullydeployed. IntheWebOneClickPublishtoolbar,clicktheProductionpublishprofile,andthenclickPublishWeb. RuntheInstructorspageinproductiontoverifythattheupdatewassuccessfullydeployed. Forarealproductionapplicationupdatethatincludesadatabasechangeyouwouldalsotypicallytaketheapplicationofflineduringdeploymentbyusingapp_offline.htm,asyousawintheprevioustutorial. DeployadatabaseupdatebyusingthedbDacFxprovider Inthissection,youaddaCommentscolumntotheUsertableinthemembershipdatabaseandcreateapagethatletsyoudisplayandeditcommentsforeachuser.Thenyoudeploythechangestotest,staging,andproduction. Addacolumntoatableinthemembershipdatabase InVisualStudio,openSQLServerObjectExplorer. Expand(localdb)\v11.0,expandDatabases,expandaspnet-ContosoUniversity(notaspnet-ContosoUniversity-Prod)andthenexpandTables. Ifyoudon'tsee(localdb)\v11.0undertheSQLServernode,right-clicktheSQLServernodeandclickAddSQLServer.IntheConnecttoServerdialogboxenter(localdb)\v11.0astheServername,andthenclickConnect. Ifyoudon'tseeaspnet-ContosoUniversity,runtheprojectandloginusingtheadmincredentials(passwordisdevpwd),andthenrefreshtheSQLServerObjectExplorerwindow. Right-clicktheUserstable,andthenclickViewDesigner. Inthedesigner,addaCommentscolumnandmakeitnvarchar(128)andnullable,andthenclickUpdate. InthePreviewDatabaseUpdatesbox,clickUpdateDatabase. Createapagetodisplayandeditthenewcolumn InSolutionExplorer,right-clicktheAccountfolderintheContosoUniversityproject,clickAdd,andthenclickNewItem. CreateanewWebFormUsingMasterPageandnameitUserInfo.aspx.AcceptthedefaultSite.Masterfileasthemasterpage. CopythefollowingmarkupintotheMainContentContentelement(thelastofthe3Contentelements):

UserInformation

" SelectCommand="SELECTUserId,UserName,CommentsFROM[Users]" UpdateCommand="UPDATE[Users]SET[UserName]=@UserName,[Comments]=@CommentsWHERE[UserId]=@UserId"> Right-clicktheUserInfo.aspxpageandclickViewinBrowser. Loginwithyouradminusercredentials(passwordisdevpwd)andaddsomecommentstoausertoverifythatthepageworkscorrectly. Closethebrowser. Deploythedatabaseupdate TodeploybyusingthedbDacFxprovider,youjustneedtoselecttheUpdatedatabaseoptioninthepublishprofile.However,fortheinitialdeploymentwhenyouusedthisoptionyoualsoconfiguredsomeadditionalSQLscriptstorun:thosearestillintheprofileandyou'llhavetopreventthemfromrunningagain. OpenthePublishWebwizardbyright-clickingtheContosoUniversityprojectandclickingPublish. SelecttheTestprofile. ClicktheSettingstab. UnderDefaultConnection,selectUpdatedatabase. Disabletheadditionalscriptsthatyouconfiguredtorunfortheinitialdeployment: ClickConfiguredatabaseupdates. IntheConfigureDatabaseUpdatesdialogbox,clearthecheckboxesnexttoGrant.sqlandaspnet-data-dev.sql. ClickClose. ClickthePreviewtab. UnderDatabasesandtotherightofDefaultConnection,clickthePreviewdatabaselink. Thepreviewwindowshowsthescriptthatwillberuninthedestinationdatabasetomakethatdatabaseschemamatchtheschemaofthesourcedatabase.ThescriptincludesanALTERTABLEcommandthataddsthenewcolumn. ClosetheDatabasePreviewdialogbox,andthenclickPublish. VisualStudiodeploystheupdatedapplication,andthebrowseropenstothehomepage. RuntheUserInfopage(addAccount/UserInfo.aspxtothehomepageURL)toverifythattheupdatewassuccessfullydeployed.You'llhavetologinbyenteringadminanddevpwd. Dataintablesisnotdeployedbydefault,andyoudidn'tconfigureadatadeploymentscripttorun,soyouwon'tfindthecommentthatyouaddedindevelopment.Youcanaddanewcommentnowinstagingtoverifythatthechangewasdeployedtothedatabaseandthepageworkscorrectly. Followthesameproceduretodeploytostagingandproduction. Don'tforgettodisabletheextrascripts.TheonlydifferencecomparedtotheTestprofileisthatyouwilldisableonlyonescriptintheStagingandProductionprofilesbecausetheywereconfiguredtorunonlyaspnet-prod-data.sql. Thecredentialsforstagingandproductionareadminandprodpwd. Forarealproductionapplicationupdatethatincludesadatabasechangeyouwouldalsotypicallytaketheapplicationofflineduringdeploymentbyuploadingapp_offline.htmbeforepublishinganddeletingitafterward,asyousawintheprevioustutorial. Summary You'venowdeployedanapplicationupdatethatincludedadatabasechangeusingbothCodeFirstMigrationsandthedbDacFxprovider. Thenexttutorialshowsyouhowtoexecutedeploymentsbyusingthecommandline. Previous Next Inthisarticle



請為這篇文章評分?