11.2: Deploying to Azure
First, if you are not registered for an Azure account, head to azure.microsoft.com.
Adding Solution to our Project
There are multiple ways to deploy to Azure. We'll use Visual Studio, because it is probably the most intuitive and less prone to error. An alternative is using the Azure Cloud CLI.
Since we created our projects in VS Code - we don't have a solution for our project like when we create one in Visual Studio. So, first - let's create a solution to hold our projects.
In the root of your project, run the following command to generate a solution file:
dotnet new sln -n EFConnect
Now, we'll add our projects to the solution. According to the docs, you should be able to run this command to add all of the projects at once:
dotnet sln todo.sln remove **/*.csproj
However, this seems to be dependent on your terminal settings, so it may not work. Alternatively, you can add them individually:
You can run this command to see what projects have been added to your solution:
Deploying the Application
Navigate to your project's folder and click the solution file to open the project in Visual Studio.
In the Solution Explorer, right click on the EFConnect.API
and choose Publish
.
In the dialog box that pops up - choose App Service
, Create New
and in the dropdown box at the bottom select Create Profile
.
Click Create Profile
and in the next dialog box - choose an app name and a resource group name and a hosting plan. If you have deployed to Azure before - you can use the same hosting plan that you've used previously.
Then select Create a SQL Database
:
Choose New
to the right of Server:
In the Configure SQL Server
dialog box - enter a Server Name
, Administrator Username
, and Administrator Password
. The credentials will be how you access your database in the cloud - separate from your Azure login.
Click Ok
and your final Create App Service
dialog box should look something like this:
Make sure you have a database, a server, and a service in the section circled above.
Click Create
and if everything goes well - you should see this screen:
Click Publish
.
Seeding the Database
Unfortunately, the UserSeedData.json
file we have been using to seed our database won't be deployed with our .Data
project.
An alternative is to generate SQL scripts and use them to seed our Azure database.
Decide if you would like to include data you used while developing or if you would like a clean version of the app from the seed data. If you would like a clean version - uncomment the seeder.SeedUsers();
line in your Startup.cs
class and run the application.
Once your local database is in a state you'd like for the deployed database to be in - open up SQL Server Management Studio.
Right click on your development database - choose Tasks
and then Generate Scripts
.
In the next window, choose Select specific database objects
and then select all of the tables except the migrations history:
Next, choose Advanced
:
Scroll down until you find Types of data to script
and select Schema and data
from the dropdown.
Click through next - and copy the generate SQL
script.
Go to the Azure Portal and login to your account.
Select the database you created previously in Visual Studio and then select Query Editor
.
Login to the database with the credentials you entered in Visual Studio in a previous step (it's not necessarily your Azure credentials).
Click New Query
:
Paste in the scripts generated previously. Delete the line at the top USE [EFConnect]
or equivalent.
Click Run
.
Your tables and associated data should be generated.
Last updated