![]() ![]() Provide the password myStong_Password123# for the default user sa that we configured in the docker-compose.yml file above. ![]() Open SSMS and type the localhost,8002 in the Server name field. This means we can remotely connect SQL Server running inside the Docker container using the SSMS installed on the host machine. If you remember, we mapped the default SQL Server port 1433 with port 8002 in the docker-compose.yml file above. Connect SQL Server Docker Container using SSMS If you will try to access your Web API in the browser, you will see an output similar to the following screenshot where the product data is returned from the database. ![]() Press F5 in Visual Studio and wait for some time and finally, you will be able to see both containers appearing in the Docker Desktop. It is now time to build and run our containers using the configurations available in the docker-compose.yml file. Running Web API and SQL Server Containers using Docker Compose "DefaultConnection": "Server=SqlServerDb Database=OnlineShopDb User Id=sa Password=myStong_Password123# Trust Server Certificate=True " Next, we need to define our database connection string and we can save the connection string in the appsettings.json file as shown below: appsettings.json Basically, we are creating a database and a Products table and we want to add some initial data in that Products table that’s why we are overriding the OnModelCreating method of DbContext in the above code which allows us to seed data. We are using EF Core Code First approach and if you are not familiar with this then I will recommend you to read my post Data Access in ASP.NET Core using EF Core (Code First). Protected override void OnModelCreating(ModelBuilder modelBuilder) Var dbCreater = Database.GetService() as RelationalDatabaseCreator Public OnlineShopDbContext(DbContextOptions options) Public class OnlineShopDbContext : DbContext Open the NuGet package manager and search and install the following packages in your project. The first thing we need is to install Entity Framework Core and SQL Server Data Provider. RUN dotnet publish "WebApiSqlServerDockerDemo.csproj" -c Release -o /app/publish /p:UseAppHost=falseĮNTRYPOINT RUN dotnet build "WebApiSqlServerDockerDemo.csproj" -c Release -o /app/build RUN dotnet restore "WebApiSqlServerDockerDemo/WebApiSqlServerDockerDemo.csproj" #See to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.įROM /dotnet/aspnet:7.0 AS baseįROM /dotnet/sdk:7.0 AS buildĬOPY NET 7.0 and make sure you enable the Docker support while you are creating the project. Let’s create a new ASP.NET Core Web API project WebApiSqlServerDockerDemo in Visual Studio 2022 using.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |