0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL Server コンテナーを実行して.NET アプリケーションと接続する

0
Posted at

参考

概要

Docker コンテナで立ち上げたSQL Serverを.NET 5.0で作成したアプリケーションに接続します。

Docker コンテナの作成

Docker Imageを取得

docker pull mcr.microsoft.com/mssql/server:2019-latest

Dockerでコンテナイメージを実行する

docker run -e ACCEPT_EULA=Y -e SA_PASSWORD=<YourStrong@Passw0rd> \
   -p 1433:1433 --name sql1 -h sql1 \
   -d mcr.microsoft.com/mssql/server:2019-latest

コンテナが無事に起動しているかを確認

>>> docker ps
CONTAINER ID   IMAGE                                        COMMAND                  CREATED          STATUS          PORTS                                       NAMES
0c832fda2fef   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   15 seconds ago   Up 11 seconds   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   sql1

起動したSQL ServerへはVS Codeの拡張機能から接続すると便利
参考:Visual Studio Code 用 SQL Server 拡張機能

VS Codeの拡張機能でSQL Serverに接続するときの認証情報
server name:localhost,1433
db name:空欄
認証方法:SQL Login
user name:sa
password:<YourStrong@Passw0rd>

.NET アプリケーションとの接続

appsetting.jsonにDBとの接続情報を書き加える

{
  "ConnectionStrings": {
    "defaultConnection": "Server=tcp:localhost,1433;Initial Catalog=sample-db;Persist Security Info=False;User ID=sa;Password=P@ssw0rd;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  },
  "Logging": {
      "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
      }
    },
"AllowedHosts": "*"
}

以下のドキュメントを参考に.NET Coreアプリからデータベースへの接続をする設定を行う

参考:EF Core の概要

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?