In this article, I will show you which application can be used to work with subd, how to connect Amazon RDS to the project, and how to use a connection string to connect, and then I will show you how to create connection with Entity Framework.
Introduction
This article is the second part of a two-part article on how to create .NET MVC Web Application using Visual Studio on Mac OS. In the last article, I showed how to create models, controllers and views.
In this article, I will show you which application can be used to work with subd
, how to connect Amazon RDS to the project, and how to use a connection string to connect, and then I will show how to use entity framework for application.
Connecting Database
You can argue for a long time how to choose a database for work. However, in order to reduce the load on our working machine and not install various applications for working with a database, I will create a new database based on an instance in my Amazon account. I have already shown how to create a new database in Amazon in one of my articles.
Immediately, we need an application that can communicate with the database on the Amazon, and the best option is VS Code. Open it and go to the Extensions section. There, we put on the SQLTools and install it (Image 1).
Image 1 - SQLTools
Next, we need to connect to our database on Amazon. Find the Endpoint of RDS
and copy it (Image 2).
Image 2 - AWS RDS Endpoint
Next, create a new connection, and select in Server Address Endpoint and rdsadmin
as a database, as well as a username and password (Image 3).
Image 3 - Connection Assistant
Although the entity framework
allows you to create a database, I prefer to create a database manually. Next, to do this, run a query (Image 4).
CREATE DATABASE hospitalServises
Image 4 - CREATE DATABASE query
After that, let's go back and change our database connection (Image 5).
Image 5 - Database test connection
We do not need the method that I described in the previous article:
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("S");
so we will create a constructor:
public VegaDbContext(DbContextOptions<VegaDbContext> options):base(options)
{
}
Each .NET Core MVC project has a file appsettings.json in which you can configure the connection, let's open this file (Image 6).
Image 6 - appsettings.json
Next, edit ConnectionStrings
, but if is not created. Next the sample of connection string:
{
"ConnectionStrings":{
"Default":"Server=YOUR ENDPOINT ,1433; Database=DATABASENAME;
Integrated Security=False;Persist Security Info=True;
Trust Server Certificate = True;User ID=LOGIN;Password=PASSWORD"
},
For this step, we are done.
Connecting Entity Framework
Since we are using the CodeFirst approach, we need to use the migration mechanism. In Visual Studio mac, we do not have a Package Manager console and we can use the terminal to create the migration. For test, let us open the project folder in the terminal and run the command (Image 7).
dotnet ef
Image 7 - EF test
Next, I want to note that it is necessary to open exactly the folder with which Startup.cs
is located, otherwise the creation of the migration will not work. To create the migration, enter the command (Image 8):
dotnet ef migrations add InitialUpdate
Image 8 - Creating migration
Next, update the database using the command (Image 9).
dotnet ef database update
Image 9 - Creating the migration
After successful completion of the database update, let's check the creation of tables. Open VS Code and open the database.
Image 10 - Tables was created
As we can see, our table is created.
Next for test, launch our application and go to Hospitals (Image 11).
Image 11 - Hospitals View
Then, create a new hospital (Image 12):
Image 12 - Creating a new hospital
Then we'll save. As you can see, everything works (Image 13).
Image 13 - Hospital has been created
Conclusion
As shown in this tip, we create a connection to database, edit connection strings and create connection in our .NET MVC application with Entity Framework which is not a difficult process if you follow all the suggested steps.
History
- 30th November, 2021: Initial version