40. 複合型(ComplexType)
40
複合型を参照しているエンティティ・クラス
public class Employee
{
public string EmployeeId { get; set; }
public Address Jusho { get; set; }
}
複数のプロパティで構成されるデータ型
主キーとナビゲーション・プロパティを持たない
public class Address
{
public string Prefectures { get; set; }
public string City { get; set; }
public string Othre { get; set; }
}
参照元のテーブルのフィールドにマッピングされる。
個別のテーブルにはマッピングされない。
テーブルのいくつかのフィールドにマッピングされる
複合型を参照しているプロパティ名 + 複合型のプロパティ名になる
41. 複合型(ComplexType)
41
Column属性で名前を指定
public class Employee
{
public string EmployeeId { get; set; }
[Column("X")]
public Address Jusho { get; set; }
}
public class Address
{
[Column("Field1")]
public string Prefectures { get; set; }
[Column("Field2")]
public string City { get; set; }
[Column("Field3")]
public string Othre { get; set; }
}
無効
42. マッピングしないプロパティ
42
NotMapped属性でマッピングしないプロパティを作成
public class Item
{
public string ItemId { get; set; }
[NotMapped]
public string ItemName { get; set; }
}
※ NotMapped属性はSystem.ComponentModel.DataAnnotations.Schema名前空間です
43. データの取得
43
基本的なデータの取得
データの取得、保存の操作はLINQ to Entitiesを利用
using (var db = new TestDB())
{
var selectQuery = from item in db.Items
where item.ItemId == "A001"
select item;
var return1 = selectQuery.Any();
var return2 = selectQuery.Single();
var return3 = selectQuery.ToList();
}
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Items] AS [Extent1]
WHERE N'A001' = [Extent1].[ItemId]
)) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
SELECT TOP (2)
[Extent1].[ItemId] AS [ItemId],
[Extent1].[ItemName] AS [ItemName],
[Extent1].[ItemType] AS [ItemType]
FROM [dbo].[Items] AS [Extent1]
WHERE N'A001' = [Extent1].[ItemId]
SELECT
[Extent1].[ItemId] AS [ItemId],
[Extent1].[ItemName] AS [ItemName],
[Extent1].[ItemType] AS [ItemType]
FROM [dbo].[Items] AS [Extent1]
WHERE N'A001' = [Extent1].[ItemId]
45. データの更新
45
基本的なデータの更新
using (var db = new TestDB())
{
var selectQuery = from item in db.Items
where item.ItemId == "A001"
select item;
var updateItem = selectQuery.Single();
updateItem.ItemName = "TestName";
updateItem.ItemType = ItemType.Type2;
db.SaveChanges();
}
SELECT TOP (2)
[Extent1].[ItemId] AS [ItemId],
[Extent1].[ItemName] AS [ItemName],
[Extent1].[ItemType] AS [ItemType]
FROM [dbo].[Items] AS [Extent1]
WHERE N'A001' = [Extent1].[ItemId]
UPDATE [dbo].[Items]
SET [ItemName] = @0, [ItemType] = @1
WHERE ([ItemId] = @2)
①データを取得
②更新内容を設定
③データソースに保存、オブジェクト
コンテキストの変更をリセットする
46. データの削除
46
基本的なデータの削除
using (var db = new TestDB())
{
var selectQuery = from item in db.Items
where item.ItemId == "A001"
select item;
var updateItem = selectQuery.Single();
db.Items.Remove(updateItem);
db.SaveChanges();
}
SELECT TOP (2)
[Extent1].[ItemId] AS [ItemId],
[Extent1].[ItemName] AS [ItemName],
[Extent1].[ItemType] AS [ItemType]
FROM [dbo].[Items] AS [Extent1]
WHERE N'A001' = [Extent1].[ItemId]
①データを取得
②オブジェクトを削除
DELETE [dbo].[Items]
WHERE ([ItemId] = @0)
③データソースに保存、オブジェクト
コンテキストの変更をリセットする
82. マイグレーション
82
Migrationsの有効化
※パッケージマネージャコンソールで行います。
[ツール(T)]->[NuGet パッケージ マネージャー(N)]->[パッケージ マネージャー コンソール(O)]
PM> Enable-Migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project Sample1.
PM>
スキャフォールディング(足場作り)
PM> Add-Migration TestDB
Scaffolding migration 'TestDB'.
The Designer Code for this migration file includes a snapshot of your current Code First model.
This snapshot is used to calculate the changes to your model when you scaffold the next migration.
If you make additional changes to your model that you want to include in this migration, then you
can re-scaffold it by running 'Add-Migration TestDB' again.
PM>
83. マイグレーション
83
データベースへの移行
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201605302350350_TestDB].
Applying explicit migration: 201605302350350_TestDB.
Running Seed method.
PM>
‘-Verbose’ でSQLの表示