Monday, October 30, 2017

My intention is to give you a practical introduction into developing ASP.NET Core 2.0 MVC and Web API apps with the SQLite database as an alternative to traditional SQL Server.A useful utility that comes in handy when working with the SQLite database is SQLiteStudio. Download SQLiteStudio from: http://sqlitestudio.pl/?act=download. Extract the ZIP file and place contents in a separate folder. Run SQLiteStudio.exe.We will build an ASP.NET Core 2.0 app that uses the following Student entity:

ASP.NET Core 2.0 MVC project

In a working directory, create a folder named SQLiteWeb. Change to the SQLiteWeb directory. Try out some of these important .NET Core 2.0 commands:

dotnet --help – this gives you a list of common commands

dotnet restore – restore dependencies specified in the .NET project

dotnet build - Builds a .NET project

dotnet run --help – provides help information about the run command

dotnet new --help – shows the types of templates that can be scaffolded. At the time of writing these are 18 different templates

dotnet new mvc --help – shows switches that can be used when creating an MVC application

We will create an MVC application that uses "Individual authentication" and the SQLite database.

Execute the following terminal command from within the SQLiteWeb directory:

dotnet new mvc --auth Individual

A web app is created for you and all Nuget packages are automatically restored. To run the application,
execute the following command inside the terminal window:

As described in the message, point your browser to http://localhost:5000 and you will see the default

ASP.NET Core page:

This runs your application in a web server called Kestrel that is listening on port 5000. Register a new user.

Stop the web server by hitting Ctrl+C. If you are curious about where the data is saved and the location of the SQLite database, you will find a *.db file located in the bin/Debug/netcoreapp2.0 directory. Have a peek at its contents using the SQLiteStudio utility mentioned earlier in this article.

To open your web application in Visual Studio, start Visual Studio then open the SQLiteWeb.csproj file.

File >> Open >> Project Solution

Hit CTRL + F5 in Visual Studio 2017. This time, the web application will start and will be hosted by IIS Express.

When working with ASP.NET Core, you will need to go to the command-line interface frequently. Add a command-prompt extension to make it easier. Click on Tools >> Extensions and Updates…

SQLite does not support this migration operation ('AddForeignKeyOperation'). For more information, see http://go.microsoft.com/fwlink/?LinkId=723262.

This error is caused by the fact the SQLite cannot alter tables and indexes during migration. See this article. The workaround is to comment out all the lines of code in the "Data/Migrations/xxxxxx_First Migration.cs" file that do not pertain to the Students entity. This should be done in both the up() and down() methods. Thereafter, run the "dotnet ef database update" command again and it should complete successfully.

Seed Data

Before we carry out code first migrations, let us first create some seed data:

In the Models folder, create a class named DummyData.cs.

Add the following Initialize() method code inside the DummyData class:

To generate seed data, we will first inject the dependency “ApplicationDbContext context” into the arguments of the Configure() method in Startup.cs.
Next, we can make a call to seed the data at the bottom of the Configure() method with the following statement:

DummyData.Initialize(context);

At this point, data will not have been seeded yet because this happens when the application is actually run.

Creating an MVC UI

Let us seed the data by running your web application in a browser. You should see the same page as we saw earlier. Let us create a UI so that we can see the seeded data.

Right-click on the Controllers folder and choose Add >> New Item… >> Controller...

Choose "MVC Controller with views, using Entity Framework" then click on Add.

Model Class=Student, Data context class=ApplicationDbContext

Click on Add. If you are asked to save the solution file then accept the default location and save it in your project root folder.

You’ll notice that the controller takes a ApplicationDbContextas a constructor parameter. ASP.NET dependency injection will take care of passing an instance of ApplicationDbContextinto your controller.

The controller contains an Index action, which displays all students in the database, and a Create action, which inserts a new student into the database.

Let us add a link to the Students controller on the main page of our application. Open _Layout.cshtml under Views/Shared.

In this example, I am using a PHP app that simply shows some cartoon characters. The output of the PHP app looks like this:

Login into portal.azure.com. On the left-side, click on “App Services”.

Click on “+ Add”.

Click on “Web app”.

On the next blade, click on the “Create” button.

Enter a unique name for App name. Choose your subscription, select to create a new Resource Group, select an App Service plan/Location then click on Create.

The web application will be created for you in less than two minutes. When it is ready, you will see it in the list of your App services.

Click on the newly created app service on the first blade. In the above example it is toon. Then, click on Deployment credentials.

Enter a unique FTP/deployment username and Password. Note that these credentials can be used for both FTP and Git. Also, these same credentials can be used for this web app as well as other web apps that you may have already created or those that you plan to create in the future. Therefore, it important that you remember these credentials. If you forget the credentials, then you can always create a new pair of username and password.

Our next step is to let Azure know that we want to deploy our web app using Git. Click on “Deployment options”.

Click on “Choose Source” then select “Local Git Repository”.

Afterwards, click on OK.

Next, we need to obtain the URL of the remote Git repository on Azure. Click on Overview.

Copy the “Git clone url” and save it in a text editor like Notepad. This will later be used to push your code from your computer to Azure. Note that you can conveniently click on a toolbar on the right of the Git URL to copy it.

Now, back on your computer, open a terminal window in the working directory of the web application that you wish to deploy. Run the following Git commands:

Right after you execute the last push command, you will be prompted to authenticate with your deployment credentials in Azure. These are the credentials that you created earlier.

Once your credentials are accepted, the push process will commence. When it is all finished, you can verify that your code was accepted by Azure by clicking on “Deployment options” in Azure.You should see a check mark beside your commit.

The true test is to go to the website and see whether or not it indeed works. Click on Overview. The website URL is on the right side of the last blade. Click on it.

The website should show up in your browser.

What next? Make a change to your code and push it to the remote Git repository on Azure. You will soon after notice that the change you made is reflected on your web application.