sql-server - 似乎无法让我的.Net Core应用程序在Docker容器中与SQL Server一起使用

标签 sql-server angular docker asp.net-core-mvc docker-compose

我正在阅读Adam Freeman撰写的“ASP.Net Core MVC的基本 Angular ”的第4章。
我一直在尝试使初始数据库在Docker容器中针对SQL Server运行。

这是他的原始docker-compose.yml文件:

version: "3"

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

当我尝试使用此文件运行应用程序时,出现错误,影响到一些东西:“找不到此sql服务器的 list ”
所以我改变了:
image: "microsoft/mssql-server-linux:ctp2 0"


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

并运行:
docker-compose up 

似乎有效。

但是现在我仍然得到:
SqlException: Cannot open database "SportsStore" requested by the login. The login failed.
Login faild for 'sa'

这是其余的设置。

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);
        }
    }
}

一切就绪后,我运行:
dotnet ef migrations add Initial

这本书没有让我们运行dotnet ef migrations update。
接下来,我们运行:
docker-compose up

启动数据库。
然后运行该应用程序,我们应该看到播种的结果。

但是相反,我收到“sa”的登录失败错误。

有人知道这是怎么回事吗?

最佳答案

原来它确实需要一个

dotnet ef migrations update

之后
docker-compose up

然后在运行应用程序以生成数据库并为其播种之前。

关于sql-server - 似乎无法让我的.Net Core应用程序在Docker容器中与SQL Server一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001530/

相关文章:

c# - 以编程方式使用 c# 删除 SQL Server 2016 表

sql - 为什么显示 SQL 语句的进度不切实际?

linux - 我在使用 docker compose 安装多个应用程序时遇到问题

docker - shell 到 Docker 容器后如何编辑文件?

docker - Elasticbeanstalk 部署失败

c# - 优化日历应用程序的查询和/或数据模型

mysql - 在迁移到 SQL Server 2008 之前将 MySQL SQL 模式设置得更严格

angular - 页面初始化时触发 ngb-raring 上的rateChange

angular - 获取 Angular 2 中的 mdslider 值?

node.js - 由于 Node.js 和 Angular 运行在不同的本地主机上,如何在 Angular 中使用 Nodemailer 电子邮件验证的路由?