This is part 23 of azure tutorial. In this video, we'll discuss how to deploy
asp.net web application with SQL server database and entity framework as the data access framework
to azure. In part 21 of this video series, we discussed how to deploy asp.net web application
that does not use a database to azure. This is a one-step process. Using visual studio "Publish"
feature, we create an App Service and App Service Plan in azure and then hit the "publish" button
to have our code deployed in azure. With just a few clicks, in just a few minutes,
we have our app up and running in azure. In part 22 of this video series, we discussed
how to deploy asp.net web application that uses SQL server database and ADO.NET as the data access
framework. This is a three-step process. First, we deploy the web application itself. Second,
we create SQL server and the database. Finally, create the database objects like tables, views,
stored procedures etc, and then seed the database tables with initial application data. In this
video, we'll discuss how to deploy ASP.NET web application with SQL server database and entity
framework as the data access framework to azure. If you're new to entity framework, please check
out our entity framework tutorial. I'll include the link in the description of this video. Now,
it is this application that we want to deploy to azure. It retrieves inspirational quotes from
a SQL server database and displays them on this web page. Notice the URL, it says localhost. So,
at the moment, this web application is running on my local machine. We want to deploy it to
azure. Now, let's take a look at the project setup. First, web.config file. Notice, here
we have the database connection string. As you can see from the connection string, at the moment
in my project, I'm using SQL Server LocalDB. If you want, you can use a full-blown SQL server,
the process is still the same. Next, we have the "Quote" class. This is a very simple class
with just two properties - "Id" and "QuoteText". In this application we are using entry framework
code first approach and here is the corresponding DBContext class and notice the name of the class
"QuoteDBContext", it has the same name as the database connection string in web.config and
this is important and at the moment, our application needs only one database table,
so within the "QuoteDBContext" class, we only have one DbSet property - DbSet.
Next, we have the "QoteRepository" class. As you can see, this class has got just one method,
GetQuotes() that returns the list of "Quote" objects and all it's doing is, creating an
instance of QuoteDBContext class, retrieving the list of Quotes and returning them and then,
it is the Default.aspx page that is responsible for displaying the list of Quotes and for that,
we have a repeater here with ItemTemplate and AlternatingItemTemplate and as you can see both
the templates bind to the QuoteText property of our Quote class and then in the code-behind file,
in the Page_Load() event, we create an instance of QuoteRepository, call the GetQodes() method,
retrieve the list of quotes, set it as the data source and call the DataBind() method.
Next, we have "QuoteDBContextSeeder" class. This class is important, because it is this class
that seeds our database tables with initial application data. When we run this application for
the first time, we will not have "Quotes" database table because we are using entity framework code
first approach. So, when we run this application for the first time, it creates "Quotes" database
table and populates it with our initial data. As you can see from the web page, when we run this
app, we see these five quotes and these quotes are coming from the "Quotes" database table and
it is this Seed() method that seeds the respective table with initial data. As you can see, we are
creating five quotes here and then adding each of the Quote objects to "QuoteDBContext" class
and then calling Seed() method on the base class. Now, when the application starts,
we want to call this database context seeder class. So, within Application_Start() event of
Global.asax, we are calling "QuoteDBContextSeeder" class using "Database.SetInitializer()" method.
Now, on my local machine, I have already ran this application. So,
notice both - the database "QuotesDB" and the "Quotes" table itself are created and
this table is also populated with the initial seed data. Now, let's delete this database.
There we go. Our database is gone. Now, let's run our application again. Entity Framework Code First
approach detects we don't have the database anymore, so it's going to do three things. First,
it creates the database itself. Next, the database table and also seeds it with our initial five
quotes. So, in just a few seconds, we should see the quotes displayed again. There we go,
we see the five quotes as expected. Now, let's go back to SQL Server Management Studio
and then refresh this databases folder. We see our "QuotesDB" database and if we expand this
and go to tables, we also see the "Quotes" database table. So, this is the application
that we want to deploy to azure. I will make this project source code available as a download. I'll
include the download link in the description of this video. At the moment, we are in the
azure portal and looking at our quotes resource group and notice, within this resource group,
we have an App Service and App Service Plan. If you remember, we created both of these from
visual studio "Publish" window in Part 21 of this video series and it is this app service that we
are going to deploy our application to. Now, in the interest of time, I'm not going to repeat
those steps since we have already covered them in Part 21 and for this video, they are exactly
the same, and if you notice, within this resource group we don't have our SQL server database. So,
let's see how to do that from within visual studio. First, get to the "Publish" window. For
that, right click on the project name in Solution Explorer and select this option - "Publish". We
want to add a database dependency, so, click the "Add" button and then select "Azure SQL database"
and then click "Next". We don't have any database in azure yet. So, we want to create a
new database. For that, click this "+" sign. Let's name the database "QuotesDB" and we don't have a
database server in azure. First, we need to create a database server. For that, click on this "new"
link. Provide a name for the database server, a location and then an administrator username,
password and then confirm the password. Click OK, and then "Create".
There we go, both our database server and the database are created. Click "Next". Provide
a name for the database connection string. We can actually select it from the drop-down list
right here. Notice, the name it has got, the same name as our DBContext class and this is important.
So, let's select this and then provide a username and password and then click "Next" and "Finish".
Let's close this wizard. All that is left to do now is, publish our code to azure. For that,
click on this "Publish" button. In a few seconds, we should see the five quotes
displayed. There we go. Take a look at the URL. Our app is up and running in azure.
So, the important point to keep in mind is, with entity framework we do not have to manually create
database tables and seed them with initial data. This is done for us automatically by the entity
framework DBContextSeeder class. That's it in this video. Thank you for listening.