Contenu connexe
Similaire à 20110607 (20)
20110607
- 1. mvcConf @:Japan
~ ASP.NET MVC ブート キャンプ ~
MVC の M
あおい情報システム株式会社
小野 修司
Microsoft MVP for ASP.NET/IIS
blogonos.wordpress.com twitter.com/onos
- 2. アジェンダ
MVC の M として何を用いるか
Entity Framework 4.1 CodeFirst
OData 連携
2
- 5. POCO クラス定義
データ構造を意識したクラス定義
主キー
ナビゲーションプロパティ、外部キー
レイジーローディング
計算項目
クラス定義=テーブル設計
設定に勝る規約(Convention over Configuration)
属性による設定
Fluent API による設定(DbContext 内)
5
- 6. Fluent API の例:[Required]
public class GroupContext : DbContext
{
public DbSet<Member> Members { get; set; }
public DbSet<Category> Categories { get; set; }
protected override
void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Member>()
.Property(m => m.Name).IsRequired();
}
}
#DB定義は同じになるが検証等のプログラム上の処理は異なる
6
- 8. 接続文字列定義
利用(構築)する DB の場所を設定
設定に勝る規約(CoC)
該当する接続文字列がない場合、 SQLExpress
インスタンスを利用
<add name=“GroupContext“
connectionString="Data Source=|DataDirectory|Group.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
8
- 9. データベースの初期化
Initializerクラス
DBの構築/修正
基底クラスにより設定
POCOクラスの定義変更を即座にDBに反映可能
項目の初期値設定
Seedメソッドのオーバーライド
常に同じデータを設定-テスト環境として有効
パッケージの初期値投入
9
- 11. データ取得
○単一データ取得
db.Members.Find(id);
○複数データ取得(リンクなし)
db.Members.ToList();
○複数データ取得(一括ローディング)
db.Members.Include(m => m.Category).ToList();
11
- 12. データ追加/更新/削除
○データ追加
db.Members.Add(member);
db.SaveChanges();
○データ更新(Attachして更新状態に変更)
db.Entry(member).State = EntityState.Modified;
db.SaveChanges();
○データ削除
Member member = db.Members.Find(id);
db.Members.Remove(member);
db.SaveChanges();
12
- 13. DropDownList の使い方
○コントローラーで SelectList を作成し、ViewBag.フィールド名
に詰め込む
ViewBag.CategoryID
= new SelectList(db.Categories, "CategoryID", "Name");
○ビューではフィールド名とオプションラベルのみ指定
@Html.DropDownList("CategoryID", String.Empty)
13
- 16. 実行環境
.NET Framework 4
MVC 3 Tools Update のテンプレート
MVC 3 の dll を参照済み
EF 4.1 の dll を参照済み
SQL Server Compactを利用可
配置可能な依存関係の追加
MVC 3、SQL Server Compact
EF 4.1 はローカルコピーを True に
16
- 22. MVC の 「 M 」
用意されているデータアクセス機能をいか
に活用するか
DbContext
ObjectContext
DataServiceContext . . .
LINQ によるデータ抽出
データの追加/更新方法は Context により異なる
22
- 24. Entity Framework
http://msdn.microsoft.com/en-us/data/gg192989
http://www.asp.net/entity-framework/tutorials
OData
http://www.odata.org/
http://msdn.microsoft.com/en-us/data/hh237663
WCF Data Services
http://msdn.microsoft.com/ja-jp/library/cc668792
24
- 25. © 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the
part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.