.Net Core Application With Azure Hosted Database

In this article, I will be explaining how to use some properties of data annotation in order to make it easier to model your database and also to save time with front-end validations.

How to create your database?
How to use data annotation in your Entities?
Why would I host my database at Azure?

"SQL Database Managed Instance provides the broadest SQL Server engine compatibility and native virtual network (VNET) support so you can migrate your SQL Server databases to SQL Database Managed Instance without changing your apps. With Managed Instance, you can combine the rich SQL Server surface area with the operational and financial benefits of an intelligent, fully managed service. Managed Instance is your best destination when migrating a large number of existing SQL Server databases from on-premises or virtual machines to SQL Database." Read more at https://azure.microsoft.com/en-us/services/sql-database/

How to do it?

First, let's set up the database.

Azure 

Azure

If you do not have a server, you must create one.

Azure 

And now configure your pricing tier, it depends 100% on how you are going to use it.

Azure 

Read more about the pricing tier,
  • https://azure.microsoft.com/en-us/pricing/details/sql-database/managed/ 
  • https://azure.microsoft.com/en-us/pricing/details/sql-database/single/
Wait until your database is configured ... it takes awhile

When your database is ready let's get the connection string.

Azure

Azure 

Your connection string probably looks like this.
  1. Server = tcp: sampleazurenetcore.database.windows.net, 1433;  
  2. Initial Catalog = SampleAzureDB;  
  3. Persist Security Info = False;  
  4. User ID = {  
  5.     your_username  
  6. };  
  7. Password = {  
  8.     your_password  
  9. };  
  10. MultipleActiveResultSets = False;  
  11. Encrypt = True;  
  12. TrustServerCertificate = False;  
  13. Connection Timeout = 30;  
After changing your username and password, it looks like this,
  1. Server = tcp: sampleazurenetcore.database.windows.net, 1433;  
  2. Initial Catalog = SampleAzureDB;  
  3. Persist Security Info = False;  
  4. User ID = sampleAdmin;  
  5. Password = XXXXX;  
  6. MultipleActiveResultSets = False;  
  7. Encrypt = True;  
  8. TrustServerCertificate = False;  
  9. Connection Timeout = 30;  
Now, let's open the Azure Firewall to accept connections from our application.

Azure 

Azure 

Azure

Azure

Now, point your Db Context to your connection string and run the PMC create commands.
  • add-migration "Initial"
  • update-database
For this example I've used the same db model as here.

This is the result,

Azure 

Congratulations! You have successfully hosted .Net Core Application's database at Azure.


Similar Articles