We will create an Address Book application. We will see all CRUD operations in Blazor with Cassandra database.
Step 1 - Create Cosmos DB account with Cassandra API
Please login to the Azure portal and choose to Create New Resource, Databases and Azure Cosmos DB.
Please choose your resource group (Create a new resource group if you do not have any resource group) and give a name to Cosmos DB account. Also, choose the geographic location for the Cosmos DB account. Make sure you have chosen Cassandra as API. Currently, there are 5 APIs available in Cosmos DB.
You can review the account details and after successful validation please click the “Create” button.
It will take some to finish the deployment. You can see the status.
After some time, Cosmos DB will be successfully deployed.
You can click “Go to resource” button and open the Cosmos DB account. Please click the “Connection String” tab and get the connection details about the Cassandra database. We will use these connection details in our Blazor application later.
We can go to the “Data Explorer” tab to create a new Keyspace now. The keyspace is like a database and tables are created under this Keyspace.
We can create a new Keyspace now.
After successful creation of Keyspace, we can create a Table now. For Table creation, we must use CQL (Cassandra Query Language) commands.
Most of the CQL commands are like SQL commands.
- CREATE TABLE IF NOT EXISTS sarathlal.addressbook
- (id text PRIMARY KEY, firstname text, lastname text,
- gender text, address text, zipcode text,
- country text, state text, phone text)
Step 2 - Create Blazor application.
Please open Visual Studio 2017 (I am using a free community edition) and create a Blazor app. Choose an ASP.NET Core Web Application project template.
We can choose Blazor (ASP.NET Core hosted) template.
Our solution will be ready in a moment. Please note that there are three projects created in our solution - “Client”, “Server”, and “Shared”.
The client project contains all the client-side libraries and Razor Views, while the server project contains the Web API Controller and other business logic. The shared project contains the commonly shared files, like models and interfaces.
By default, Blazor created many files in these three projects. We can remove all the unwanted files like “Counter.cshtml”, “FetchData.cshtml”, “SurveyPrompt.cshtml” from Client project and “SampleDataController.cs” file from Server project and delete “WeatherForecast.cs” file from shared project too.
We can add “CassandraCSharpDriver” NuGet package to Shared project and server project. Both projects need this package. This package is used for creating connectivity between Blazor app and Cassandra.
As I mentioned earlier, we will create an Address Book application. We can create AddressBook model class now. Please create a “Models” folder in the Shared project and create AddressBook class inside this folder.
Please copy below code and paste to a new class.
AddressBook.cs