Sam
Sam

Reputation: 5647

Can't seem to get my .Net Core App to work with SQL Server in Docker Container

I am reading Chapt 4 of "Essential Angular for ASP.Net Core MVC" by Adam Freeman. And I am following along trying to get the initial DB to run against SQL Server in a Docker Container.

Here is his original docker-compose.yml file:

version: "3"

services:
  database:
    image: "microsoft/mssql-server-linux:ctp2 0"
    ports:
      - 5100:1433
    environment: 
      - ACCEPT_EULA=Y
      - SA_PASSWORD=mySecret123

When I try to run the application with this file I was getting an error something to the affect: "no manifest found for this sql server" So I changed:

image: "microsoft/mssql-server-linux:ctp2 0"

to

image: "microsoft/mssql-server-linux:latest"

and running:

docker-compose up 

seems to work.

But now I still get:

SqlException: Cannot open database "SportsStore" requested by the login. The login failed.
Login faild for 'sa'

Here is the rest of the setup.

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Microsoft.EntityFrameworkCore": "Information",
      "Microsoft.AspNetCore.NodeServices": "Information",
      "Default": "Warning"
    }
  },
  "Data": {
    "Products": {
      "ConnectionString": "Server=localhost,5100;Database=SportsStore;User Id=sa;Password=mySecret123;MultipleActiveResultSets=true"
    }
  }
}

DataContext:

using Microsoft.EntityFrameworkCore;

namespace SportsStore.Models
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> opts)
            : base(opts) { }

        public DbSet<Product> Products { get; set; }
        public DbSet<Supplier> Suppliers { get; set; }
        public DbSet<Rating> Ratings { get; set; }
    }
}

StartUp.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.SpaServices.Webpack;
using SportsStore.Models;
using Microsoft.EntityFrameworkCore;

namespace SportsStore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(options =>
                options.UseSqlServer(Configuration["Data:Products:ConnectionString"]));
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, DataContext context)
        {
            app.UseDeveloperExceptionPage();
            app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
            {
              HotModuleReplacement = true
            });

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            SeedData.SeedDatabase(context);
        }
    }
}

Once everything is in place I run:

dotnet ef migrations add Initial

Then the book does not have us run dotnet ef migrations update. We next run:

docker-compose up

to start the DB. Then run the application and we are supposed to see the results of some seeding.

But instead I get the login failed error for 'sa'.

Anyone know what is going on here?

Upvotes: 1

Views: 511

Answers (2)

Ariel Malov
Ariel Malov

Reputation: 1

You have to look is updating listings in GitHub for Core 2.0

Upvotes: 0

Sam
Sam

Reputation: 5647

Turns out it does need a

dotnet ef migrations update

right after

docker-compose up

and right before running the application to generate the DB and seeding it.

Upvotes: 1

Related Questions