PerformingOptical Character Recognition(OCR) on an image is a complex task. This topic explains step by step that how to extract text from image in C# quickly and easily. By usingAspose.OCR for .NETyou can simply read characters from image in C# in a few steps.
The approach is same for any type of .NET application if you’re using C# language. Whether you want to extract all the text from the image, or read text line by line from an image, Aspose.OCR for .NET can help you do that.
In this tutorial we will perform CRUD Operations in as ASP.NET Core Docker app with SQL Server as a backend. We will use Docker Compose for this so there will be 2 containers one running the ASP.NET Core app and other SQL Server 2019.
Note that this setup is very useful for people who don’t want to install SQL Server on their PC and can save the licence money. The SQL Server 2019 image will be run from docker container and is just around 350 mb in size. You can even connect to this SQL Server container from SQL Server Management Studio and do any type of work like creating database, creating table, running queries and so on.
Pull and run the SQL Server 2019 Image
Open the command prompt and run the following command which will pull the SQL Server 2019 image from Microsoft Container Registry(mcr).
1. Creates a container by the name of mysqlserver which will run this image of SQL Server.
2. The SQL Server password is set to vV5r9tn0M4@. Note that the password which we set should be strong and at least 8 character in length. It should have a combination of letters, number, capital letters and special characters.
3. The container’s port mappings is done. See -p 1440:1433, Here 1433 is port of the container (second value) which is exposed to 1440 (first value) port on the host.
Now check the docker desktop which will show this container up and running, see the below image containing the screenshot of our docker desktop.
Alternately, we can also run the docker ps on the command prompt to see this container in running mode.
Connect to SQL Server on the container with SSMS
Now we can connect with the SQL Server running in the docker container. So, in SQL Server Management Studio (SSMS), enter the following:
1. localhost,1440 for the server name, recall 1440 was the port we set earlier. Instead of localhost we can also write your machine ip which we can get from running “ipconfig” command.
2. SA for login.
3. vV5r9tn0M4@ for password.
Click the Connect button to connect with this SQL Server running in the container.
Once we are connected, we can create databases, tables, stored procedures and do any other stuff which we normally do in the database.
Alternatively, we can also use SQL Server Object Explorer windown in Visual Studio to connect to the database. Find this from the “View” menu in Visual Studio.
Changing the Database Password
We can also change the SQL Server’s password by running the following given command:
docker exec -it mysqlserver /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "vV5r9tn0M4@" -Q "ALTER LOGIN SA WITH PASSWORD='abD5r9tn0M@4'"
So, after running this command the new password becomes abD5r9tn0M@4.
Creating CRUD operations in ASP.NET Core app
Let us now Create CRUD operations in an ASP.NET Core app which uses this SQL Server running on the docker container. So create a new ASP.NET Core Web Application in visual studio.
Next, give the app a name, we have named it as DockerCrud, and un-check the option Place solution and project in the same directory.
Do not select Enable Docker Support.
Next, select the template called ASP.NET Core Web App (Model-View-Controller) for creating a basic ASP.NET Core MVC app.
Movie Database and Entity Framework Core
We will be creating a Movie Database called MovieDB in the SQL Server running on the docker container. There will be one table called as Movie, in this table Create, Read, Update and Delete operations will be performed from ASP.NET Core MVC app. The database creation and CRUD operations will be performed by Entity Framework Core (EF Core).
Note that here the ASP.NET Core MVC app will be accessing the SQL Server running on the container. Check the below image which describes this architecture.
So, first add the necessary packages of EF Core on the ASP.NET Core MVC app from NuGet. These packages are:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Next, inside the Models folder create a new class called Movie.cs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel.DataAnnotations;
usingSystem.Linq;
usingSystem.Threading.Tasks;
namespaceDockerCrud.Models
{
publicclassMovie
{
[Required]
publicintId { get; set; }
[Required]
publicstringName { get; set; }
[Required]
publicstringActors { get; set; }
}
}
Add another class called MovieContext.cs to the Models folder. It will serve as a Database Context for EF Core. The code is given below:
After that create 3 Views (Index.cshtml, Create.cshtml, Update.cshtml) inside the Views >> Home controller. These views will form the UI for the CRUD operations.
Open the appsettings.json file and create the connection string to the SQL Server database. The Data Source should be the localhost, 1440 and Initial Catalog is the name of the database. You can name the database anything you like.
Instead of localhost we can also write our pc’s IP address. The IP address can be seen by running ipconfig command on the command prompt. In our case our pc’s ip address is 192.168.1.100, so we can use it on the connection string as shown below.
Finally, we need to run EF Core migration commands on the Package Manager Console window in VS. Run these commands from the directory of the project.
dotnet ef migrations add Migration1
dotnet ef database update
This will create the database called MovieDB on the SQL Server running in the container, now we can perform CRUD operation with the ASP.NET Core MVC app.
We can also skip the migration part and use the “EnsureCreated” method on the startup or program class (depending upon our version of DOT NET). This command will create the database for us when the app runs for the first time. See – Entity Framework Core Seed Data where we have explained how this works.
Testing CRUD Operations
Now it’s time to test the workings of the CRUD operations. Run your ASP.NET Core MVC app in VS and go to Create Movie section, enter a movie name and it’s actors (separated by comma), and then click the Create button. The movie record will be inserted to the database. See the below image where we have shown this thing.
Next, click the update link and enter new values in the text box. Then click the update button for updating the record. Check the below screenshot where we have shown the update procedure.
Next click the delete button against a record to delete it. Check the below screenshot:
We have successfully tested the CRUD operations which are working perfectly. Remember the SQL Server database is running from a Docker Container.
Docker Compose for ASP.NET Core app and SQL Server Database
Now we will run ASP.NET Core MVC app and SQL Server database from Docker multi-containers using Docker Compose.
Kindly note that here we will start from fresh since Docker Compose will create a new container for running an SQL Server instance. So you can stop the older SQL Server container which you created earlier.
Start by right clicking the Project name in Solution Explorer and select Container Orchestrator Support.
Next, on the new window that opens up select “Docker Compose”.
Next, select the Target OS as Linux and click the OK button.
This will add a new project called docker-compose on the solution explorer.
Open the docker-compose.yml file which is inside the docker-compose project and add new service to it. Name this service sqldata.
Specify the image to be sql server 2019, password and expose ports. See the highlighted code given below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: '3.4'
services:
dockercrud:
image: ${DOCKER_REGISTRY-}dockercrud
build:
context: .
dockerfile: DockerCrud/Dockerfile
sqldata:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- SA_PASSWORD=vV5r9tn0M4@
- ACCEPT_EULA=Y
ports:
- "1450:1433"
Notice the ports 1450:1433, the first port is the host’s port and the second is the port of the container running sql server (here the SQL Server used is different that the previous one, you can use the older one if you require). Now we have to create the database from Entity Framework Core Migration commands. We will do it in just a moment.
As soon you save the yml file (by pressing CTRL+S), docker will create 2 containers – one will run the ASP.NET Core MVC app while the other one will run the SQL Server. The below image explains this thing.
You can see these multi containers up and running in your docker desktop.
Next change the connection string by providing Data Source=sqldata which is the service name for the SQL Server in docker-compose.yml file. This is done because containers in docker compose can communicate with one another by their names. See the asp.net core docker connection string given below after the change.
This is a great relief since we don’t have to put the IP address now. The app is ready to be run from docker containers but we have to do once more thing which is the creation of the database from migration command. The next section is going to cover it.
Running the EF Core Migrations
The EF Core migrations will create the database on the SQL Server. There is a catch when performing migrations, you have to change the database string to use localhost, 1450 instead of sqldata for Entity Framework Core to communicate with the database. Therefore the updated database connection string should be:
Now in the package manager console window run the following 2 migration command from the directory of project file.
dotnet ef migrations add Migration2
dotnet ef database update
Once the migrations are completed revert the connection string to its previous state i.e. the one using the SQL Server Service name given in the docker compose yml file.
One thing to also note that the database container should be in running mode else migrations will not run. If they are not running then press F6 to build the app and this will run the database container, and then your migrations will run successfully.
Running the APP
Now you can run your app in visual studio and start doing the CRUD operations. Press F5 in Visual Studio to run the app and enjoy.
We have created a small 15 seconds video which shows the working. Check it.
If you want to run the app directly on the browser without taking the aid of Visual Studio then you will need to expose the ports and generate HTTPS development certificate. Then map the SSL Certificate to the container using Docker Volume. We have done these things in the previous tutorial. If you want to know these steps then follow Exposing ports and configuring Environment variables for HTTPS.
In this tutorial you learned how to create a docker container with SQL Server 2019 image. This procedure is helpful if you don’t want to install sql server on your system. You also learned how to run database driven ASP.NET Core MVC app with SQL Server 2019 container using Docker Compose. We also covered asp.net core docker connection string concepts in details.
If you love reading this tutorial makes sure to do a bit of promotion by sharing it on your facebook and twitter accounts.