sql-server - SQL Server与Docker和Entity Framework的连接

标签 sql-server entity-framework docker asp.net-core database-connection

我正在尝试为要在docker上部署的服务设置数据库连接,该服务和数据库都在docker化。

将有效载荷发布到服务的API时,出现以下错误:

SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)



API定义为:
    [HttpPost]
    public async Task<IActionResult> Postfoo([FromBody] Foo foo)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        _context.Foos.Add(foo);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetFoo", new { id = foo.ID }, foo);
    }

appsettings.json中,数据库连接定义如下:
"ConnectionStrings": {
    "DefaultConnection": "Server=tcp:127.0.0.1,1433;Trusted_Connection=True;MultipleActiveResultSets=true;User ID=sa;Password=Pass@word"
},

定义数据库连接的Startup.cs中的启动逻辑如下:
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddDbContext<WorkflowContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }

docker-compose.yml中,数据库服务定义为:
version: '3.4'

services:

  sql.data:
    image: microsoft/mssql-server-linux:2017-latest

docker-compose.override.yml中,与数据库有关的部分定义为:
version: '3.4'

services:

  sql.data:
    environment:
      - SA_PASSWORD=Pass@word
      - ACCEPT_EULA=Y
    ports:
      - "1433:1433"

我可能已经忽略了,尽管仍然看不到缺少了什么?

最佳答案

为了在两个容器之间共享网络,我们可以使用docker-compose来创建共享网络。

请按照以下步骤操作:

  • docker-compose
    version: '3.4'
    
    services:
    coredocker:
        image: coredocker
        build:
        context: .
        dockerfile: CoreDocker/Dockerfile
    sql.data:
        image: microsoft/mssql-server-linux:2017-latest
        environment:
        - SA_PASSWORD=Pass@word
        - ACCEPT_EULA=Y
        ports:
        - "1433:1433"
    
  • appsettings.json
    {
        "ConnectionStrings": {
            "DefaultConnection": "Server=sql.data,1433;Database=CoreDocker;MultipleActiveResultSets=true;User ID=sa;Password=Pass@word"
        },
        "Logging": {
            "LogLevel": {
            "Default": "Warning"
            }
        },
        "AllowedHosts": "*"
    }
    

  • 这里有两点:
  • Server=tcp:127.0.0.1,1433;更改为Server=sql.data,1433,即服务名称和端口
  • 删除Trusted_Connection=True;
  • 关于sql-server - SQL Server与Docker和Entity Framework的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283909/

    相关文章:

    c# - Entity Framework Code First Auditing 多对多和一对多问题

    .net - EF 4.1,代码优先 : Is there an easy way to remove ALL conventions?

    ssl - 如何在 docker swarm 中为 swarm 集群中的特定容器启用 JMX?

    bash - 带有值的Docker命令行参数,何时使用空格与何时使用等号[duplicate]

    sql-server - 当某些行不存在时如何计算平均值?

    sql-server - SQL Server 2005 中的不可见外键

    c# - 在 mvc3 上保存 null DateTime

    python - 警告 : Running pip as the 'root' user

    sql - 如何从 nvarchar(max) 类型的列中存储和提取 XML 信息,并在连接中使用它?

    sql-server - 恢复存储过程