環境
- Visual Studio
- .Net Framework 4.8
- EnityFramwork 6
- Sqlite
インストール
nugetでSystem.Data.SQLite.EF6.Migrations
をインストール
connectionStringの設定
App.configの<configuration>
内に追加
App.config
<connectionStrings>
<add name="SampleDb"
connectionString="Data Source=.\SamleDb.db;Initial Catalog=SampleDb;"
providerName="System.Data.SQLite" />
</connectionStrings>
マイグレーションの有効化
DbContextを継承したクラスを作成
SampleContext.cs
using System.Data.Entity;
namespace Sample.Models
{
public class SampleContext: DbContext
{
public SampleContext(): base("name=SampleDb")
{
}
}
}
パッケージマネージャで以下のコマンドを実行
PM
Enable-Migrations
作成されたMigrations/Configuration.cs
にSetSqlGeneratorを追加
- Configuration.csが作成されないことがあったのでその場合はソリューションマネージャから作成
Migrations/Configuration
namespace Sample.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.SQLite.EF6.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Sample.Models.SampleContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
// 以下を追加
SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());
}
protected override void Seed(Sample.Models.SampleContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
Modelの作成
Oders.cs
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Sample.Models
{
public class Order
{
// 自動で主キーに設定される
public int Id { get; set; }
// 外部キー 1対1
[Required]
public Customer Customer { get; set; }
// 外部キー 1対多
[Required]
public List<OrderDetail> OrderDetails { get; set; } = new List<OrderDetail>();
}
}
Customer.cs
using System.ComponentModel.DataAnnotations;
namespace Sample.Models
{
public class Customer
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
}
OrderDetail.cs
namespace Sample.Models
{
public class OrderDetail
{
public int Id { get; set; }
public string Remark { get; set; }
public virtual Order Order { get; set; }
}
}
DbContextを継承したクラスに追記
SampleContext.cs
amespace Sample.Models
{
public class SampleContext: DbContext
{
public SampleContext() : base("name=SampleDb")
{
}
public SampleContext(DbConnection connection) : base(connection, true)
{
}
// 以下を追加
public DbSet<Order> Orders { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
public DbSet<Customer> Customers { get; set; }
}
}
マイグレーションの作成
パッケージマネージャで以下のコマンド実行
InitialMigration
は変更内容を表したユニークな名前を付ける
PM
Add-Migration InitialMigration
xxxxxxx-InitialMigration.cs
ファイルが作成させる(xxxxxxxはタイムスタンプ)
作成されたファイル
xxxxxxx-InitialMigration.cs
namespace Sample.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class InitialMigration : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Customers",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 2147483647),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.OrderDetails",
c => new
{
Id = c.Int(nullable: false, identity: true),
Remark = c.String(maxLength: 2147483647),
Order_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Orders", t => t.Order_Id)
.Index(t => t.Order_Id);
CreateTable(
"dbo.Orders",
c => new
{
Id = c.Int(nullable: false, identity: true),
Customer_Id = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Customers", t => t.Customer_Id, cascadeDelete: true)
.Index(t => t.Customer_Id);
}
public override void Down()
{
DropForeignKey("dbo.OrderDetails", "Order_Id", "dbo.Orders");
DropForeignKey("dbo.Orders", "Customer_Id", "dbo.Customers");
DropIndex("dbo.Orders", new[] { "Customer_Id" });
DropIndex("dbo.OrderDetails", new[] { "Order_Id" });
DropTable("dbo.Orders");
DropTable("dbo.OrderDetails");
DropTable("dbo.Customers");
}
}
}
マイグレーションの適応
データベースに変更を適応する
パッケージマネージャで以下のコマンド実行
PM
Update-Database