はじめに
この記事はPostgresでBlazorを使えるようにするまでの記事です。
NuGetでインストールが必要なもの
- Npgsql.EntityFrameworkCore.PostgreSQL
- Npgsql.EntityFrameworkCore.PostgreSQL.Design
コード側の変更点
- Server側のStartup.csファイルの
UseSqlServer
をUseNpgsql
に以下を変更
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection")));
- appsettings.json
DefaultConnection
の部分、接続先をPostgresのDBへ設定
Data/Migrations
の中のファイルを全て削除
このフォルダにはコマンドAdd-Migration InitialMigration
などで作成したファイル(DB構成の変更情報)が格納されるが、デフォルトではSQLServerのDBを更新する内容でありPostresにあったものではないため削除をする。
パッケージマネージャーコンソールでコマンド実行
-
Add-Migration InitialMigration
を実行し、Postgres用のMigrationファイルを生成する。 -
Update-Database
を実行しMigrationファイルを実行
動作確認
アカウント作成が出来れば正常にPostgresを使えている。
[Blazor関連のリンク] [Blazor WebAssembly プロジェクト作成(認証あり)](https://qiita.com/pero_88/items/be142d5d0ba92e5c91d0) [Blazor WebAssembly 初期プロジェクト構成の入門](https://qiita.com/pero_88/items/ced1028ad536a43fec1d) [Blazor WebAssembly Postgresを使うまで](https://qiita.com/pero_88/items/23e88a1d2bc3659b9946) [Blazor WebAssembly コードビハインド](https://qiita.com/pero_88/items/be871f4ac63868f048f0) [Blazor WebAssembly InputSelectの使い方](https://qiita.com/pero_88/items/3eddcd1aedf8bbc8441b)