0
1

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 5 years have passed since last update.

.NET CoreのDatabase First Migration

Last updated at Posted at 2020-04-17

.NET Coreでは2020年4月現在、VisualStudioのGUIで既存のDBからモデルを生成できない。

ということで、CUIでデータベースファーストマイグレーションを行う。

DBはSQL Serverです。

CLIツールをインストールする

EntityFrameworkCoreのCLIツールがインストールされていなければインストールします

Install-Package Microsoft.EntityFrameworkCore.Tools

SQLServerをインストール

SQL Serverがインストールされてなければインストールします。

Install-Package Microsoft.EntityFrameworkCore.SqlServer

ModelをScafoldするコマンドを実行する

このコマンドを実行することで、DBからエンティティを解析して、DbContextなど、モデル情報を生成してくれます。
2回目以降は-Forceオプションを付けます。
また、コンパイルが通る状態でないと失敗します。

Scaffold-DbContext "{接続文字列}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

参考リンク

Doesn't mention Database First approach · Issue #16897 · dotnet/AspNetCore.Docs
EF Coreツールリファレンス(Package Manager Console)-EF Core | Microsoftドキュメント

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?