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.
If you do not have a server, you must create one.
And now configure your pricing tier, it depends 100% on how you are going to use it.
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.
Your connection string probably looks like this.
- Server = tcp: sampleazurenetcore.database.windows.net, 1433;
- Initial Catalog = SampleAzureDB;
- Persist Security Info = False;
- User ID = {
- your_username
- };
- Password = {
- your_password
- };
- MultipleActiveResultSets = False;
- Encrypt = True;
- TrustServerCertificate = False;
- Connection Timeout = 30;
After changing your username and password, it looks like this,
- Server = tcp: sampleazurenetcore.database.windows.net, 1433;
- Initial Catalog = SampleAzureDB;
- Persist Security Info = False;
- User ID = sampleAdmin;
- Password = XXXXX;
- MultipleActiveResultSets = False;
- Encrypt = True;
- TrustServerCertificate = False;
- Connection Timeout = 30;
Now, let's open the Azure Firewall to accept connections from our application.
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,
Congratulations! You have successfully hosted .Net Core Application's database at Azure.