LoginSignup
0
0

More than 1 year has passed since last update.

(2021/07/22)NET CoreとSQL ServerでEntityFrameworkを使用する(DB first)

Posted at

DB firstと言いながらも、とりあえずDBだけ作って、とりあえず接続だけしてみたいってときのメモ。

環境

・Visual Studio 2019
・SQL Server 2019

プロジェクトの作成

ASP.NET Core Webアプリを選択。

image.png

Nugetでパッケージの取得

必須かどうかわからないけど、とりあえず以下をインストール。
ただし、DesignとSqlServerだけは、インストールしなかったときにエラーが出たので少なくともこの2つは必要

image.png

PMでコマンド実行

VS2019の画面下部にあるパッケージマネージャーコンソールで以下のコマンドを実行。

dotnet ef dbcontext scaffold "Data Source=<コンピューター名>\<データベース名>;Database=<データベース名>;persist security info=True;user id=<DBのユーザー名>;password=<DBのユーザーパスワード>;MultipleActiveResultSets=True;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -o Models --project <プロジェクト名>

これも、引数が本当に全部必要なのか確認したわけじゃないし、なんなら無駄な引数も入ってそうだけど、とりあえずこれで動いた。
コマンド実行後、ModelsフォルダーにDbContextのファイルが生成される。

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