VS Code+.Netでアプリを作る(No.8):.Net Coreを使ってMySQL接続(実装編)
ココに沿って勉強します。
モデルを作成
ContactBook/Models/Contact.cs
DBコンテキストを作成
モデルとDBをつなぐDBコンテキストを作成します。
ContactBook/Models/ContactBookContext.cs
Entity Framework(EF)では,DBへのデータの挿入,読み出し,更新,削除といった基本的な操作はDbContextを通じて行います。
DBコンテキストを登録
Startup.csにDBコンテキストを登録します。
MySQLの接続文字列はAppSettingsに設定しました。
ContactBook/Startup.cs
ContactBook/Startup.cs
ContactBook/appsettings.json
マイグレーション
データベースを作成します。
ターミナル
スキャフォールディング
asp-condegeneratorでコントローラーを作成します。
ついでに既定のビューも作成します。
※Microsoft Document dotnet aspnet-codegeneratorにパラメータの説明が記載されている。
エラー発生。
ここでinstall字に失敗した
$ dotnet add package Microsoft.SqlServer
のMicrosoft.SqlServerが必要になってくるのか。。
次回に続く。
モデルを作成
ContactBook/Models/Contact.cs
namespace ContactBook.Models { public class Contact { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Phone { get; set; } public string Address { get; set; } } }
DBコンテキストを作成
モデルとDBをつなぐDBコンテキストを作成します。
ContactBook/Models/ContactBookContext.cs
using Microsoft.EntityFrameworkCore; namespace ContactBook.Models { public class ContactBookContext : DbContext { public ContactBookContext(DbContextOptions<ContactBookContext> options) : base(options) { } public DbSet<Contact> Contact { get; set; } } }※DbContextはEntity Framework APIの重要なクラス。
Entity Framework(EF)では,DBへのデータの挿入,読み出し,更新,削除といった基本的な操作はDbContextを通じて行います。
DBコンテキストを登録
Startup.csにDBコンテキストを登録します。
MySQLの接続文字列はAppSettingsに設定しました。
ContactBook/Startup.cs
using ContactBook.Models; using Microsoft.EntityFrameworkCore; using Pomelo.EntityFrameworkCore.MySql.Infrastructure; using Pomelo.EntityFrameworkCore.MySql.Storage;
ContactBook/Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext<ContactBookContext>( options =>options.UseMySql( Configuration.GetConnectionString("ContactBook"), mySqlOptions => mySqlOptions.ServerVersion( new ServerVersion(new Version(8, 0, 19), ServerType.MySql) ) ) ); }
ContactBook/appsettings.json
{ "ConnectionStrings": { "ContactBook": "Server=192.168.10.10;Database=homestead;User=devadmin;Password=****************" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" }VirtualBoxのhomesteadを使っているので接続先は少し変えてます。
マイグレーション
データベースを作成します。
ターミナル
$ dotnet ef migrations add InitialCreate Build started... Build succeeded. Done. To undo this action, use 'ef migrations remove'
$dotnet ef database update Build started... Build succeeded.VMのDBを見てみるとテーブルができています。
Done.
スキャフォールディング
asp-condegeneratorでコントローラーを作成します。
ついでに既定のビューも作成します。
※Microsoft Document dotnet aspnet-codegeneratorにパラメータの説明が記載されている。
$ dotnet aspnet-codegenerator controller -name ContactsController -m Contact -dc ContactBookContext -outDir Controllers -udl -scripts Building project ... Finding the generator 'controller'... Running the generator 'controller'... To scaffold, install the following Entity Framework core packages and try again: Microsoft.EntityFrameworkCore.SqlServer. at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args) RunTime 00:00:04.46
エラー発生。
ここでinstall字に失敗した
$ dotnet add package Microsoft.SqlServer
のMicrosoft.SqlServerが必要になってくるのか。。
次回に続く。
コメント
コメントを投稿