このチュートリアルでは、Entity Framework Core を使用して SQLite データベースに対してデータ アクセスを実行する .NET Core コンソール アプリを作成します。
このチュートリアルに従うには、Windows 上の Visual Studio を使用するか、Windows、macOS、または Linux の .NET CLI を使用します。
[前提条件]
以下のソフトウェアをインストールします。
新しいプロジェクトを作成する
dotnet new console -o EFGetStarted
cd EFGetStarted
Entity Framework Core をインストールする
EF Core をインストールするには、対象とする EF Core データベース プロバイダーのパッケージをインストールします。 このチュートリアルでは、.NET がサポートするすべてのプラットフォームで実行されるため、SQLite を使用します。 使用可能なプロバイダーの一覧については、「 データベース プロバイダー」を参照してください。
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
モデルを作成する
モデルを構成するコンテキスト クラスとエンティティ クラスを定義します。
- プロジェクト ディレクトリで、次のコード を使用してModel.cs を作成します。
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public string DbPath { get; }
public BloggingContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = System.IO.Path.Join(path, "blogging.db");
}
// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; } = new();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
EF Core では、既存のデータベースからモデルを リバース エンジニアリング することもできます。
ヒント:このアプリケーションは、明確にするために意図的に物事をシンプルに保ちます。 運用環境 のアプリケーションのコードに接続文字列を格納しないでください。 また、各 C# クラスを独自のファイルに分割することもできます。
データベースを作成する
次の手順では 移行を 使用してデータベースを作成します。
次のコマンドを実行してください:
dotnet tool install --global dotnet-ef dotnet add package Microsoft.EntityFrameworkCore.Design dotnet ef migrations add InitialCreate dotnet ef database update
これにより、 dotnet ef と、プロジェクトでコマンドを実行するために必要なデザイン パッケージがインストールされます。
migrations
コマンドは、移行をスキャフォールディングして、モデルのテーブルの初期セットを作成します。database update
コマンドによってデータベースが作成され、新しい移行が適用されます。
作成、読み取り、更新、削除 &
Program.csを開き、内容を次のコードに置き換えます。
using System; using System.Linq; using Microsoft.EntityFrameworkCore; using var db = new BloggingContext(); // Note: This sample requires the database to be created before running. Console.WriteLine($"Database path: {db.DbPath}."); // Create Console.WriteLine("Inserting a new blog"); db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" }); await db.SaveChangesAsync(); // Read Console.WriteLine("Querying for a blog"); var blog = await db.Blogs .OrderBy(b => b.BlogId) .FirstAsync(); // Update Console.WriteLine("Updating the blog and adding a post"); blog.Url = "https://devblogs.microsoft.com/dotnet"; blog.Posts.Add( new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" }); await db.SaveChangesAsync(); // Delete Console.WriteLine("Delete the blog"); db.Remove(blog); await db.SaveChangesAsync();
アプリを実行する
dotnet run
次のステップ
.NET