Václav Dajbych Microsoft Student Partner dajbych.net @Dajbych feed.dajbych.net events.dajbych.net.

Post on 28-Mar-2015

215 views 1 download

Transcript of Václav Dajbych Microsoft Student Partner dajbych.net @Dajbych feed.dajbych.net events.dajbych.net.

Code-First Entity Framework

Model-View-ViewModelVáclav Dajbych

Microsoft Student Partner

dajbych.net

@Dajbych

feed.dajbych.net

events.dajbych.net

ObsahCode-First Entity Framework

RelaceSyntaxeAnotaceDemo

Model-View-ViewModelSrovnání s MVCBindingDemo

Entity FrameworkORM

Automatické mapováníTříd na tabulkyVazeb tříd na relace tabulek

ProxySledování změn

Propagace změn do databáze voláním metody

Entity Framework 4.1

announcing

Entity FrameworkCode-First

Databáze

Schéma Třídy

Databáze Třídy

Entity Framework4.1

V současnosti EF 4.1 RCGo-live licenceZa dva týdny RTW

SQL ServerSQL Server Compact 4Oracle

Navigation Property

Navigation Property

var house = (from h in db.Houses select h).First();

var town = house ;.Town

Navigation Property1:1

class Town {

int Id { get; set; }

  string Name { get; set; }

House House { get; set; }

}class House {

int Id { get; set; }

  int Number { get; set; }

Town Town { get; set; }

}

Navigation Property1:N

class Town {

int Id { get; set; }

  string Name { get; set; }

ICollection<House> Houses { get; set; }

}class House {

int Id { get; set; }

  int Number { get; set; }

Town Town { get; set; }

}

Navigation PropertyM:N

class Town {

int Id { get; set; }

  string Name { get; set; }

ICollection<House> Houses { get; set; }

}class House {

int Id { get; set; }

  int Number { get; set; }

ICollection<Town> Towns { get; set; }

}

Datové typy

intint?stringdecimalDateTimeboolbool?bytebyte[]

int, not nullintnvarchar(128)decimal(18,2), not nulldatetimebit, not nullbittinyint, not nullvarbinary(max)

Syntaxe

public class Town {  public int Id { get; set; }  public string Name { get; set; }  public virtual ICollection<House> Houses { get;  set;  }     }class Entity : Town {  public Entity() {...}  public override ICollection<House> Houses {    get {...}    set {...}  }}

Syntaxe

public class Table { public int Id { get; set; } public int? Value1 { get; set; } public string Value2 { get; set; }

public decimal Value3 { get; set; } public decimal? Value4 { get; set; }  public bool Value5 { get; set; }  public bool? Value6 { get; set; }   public byte Value7 { get; set; }          public byte[] Value8 { get; set; }  public DateTime Value9 { get; set; }}

Connection String

public class DemoDb : DbContext {  public DemoDb() : base(@"connection string") {    var initializer =  new DropCreateDatabaseIfModelChanges<DemoDb>();

   Database.SetInitializer(initializer);    Database.CreateIfNotExists();          }  public DbSet<Table> Table { get; set; }}

web.config

<connectionStrings> <add name="DatabaseCS" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=db.sdf" /></connectionStrings>

: base("name=DatabaseCS")

Inicializační strategie

DropCreateDatabaseAlwaysDropCreateDatabaseIfModelChangesCreateDatabaseIfNotExistsIgnoreDatabaseSchema

DbContext

DbContextOnModelCreatingShouldValidateEntity ValidateEntity

IDatabaseInitializerSeed

SqlCommand

DbContext.ExecuteSqlCommand( "UPDATE t SET c = {1} WHERE id = {0}", 7, DateTime.Now);

Anotace

[Key] // primary key [NotMapped][MaxLength] // nvarchar(max)[StringLength(40)] // nvarchar(40) [Required]

Entity Framework 4.1

demo

Model-View-Controller

Model

View Controller

Model-View-ViewModel

View

ViewModel

Model

WPFBinding

Každý ovládací prvek má svůj datový kontextJak ovládací prvek tak i datový kontext vyvolávají události, pokud se hodnota změní

INotifyPropertyChangedINotifyCollectionChangedDependencyPropertyObservableCollectionICommand

WPFBinding

Analogie s ASP.NET<Button Content="{Binding Text}" /><Button><%= Text %></Button>

public string Text { get; set; }code behind pro oba případy stejný

Model-View-ViewModel

demo

ObsahCode-First Entity Framework

RelaceSyntaxeAnotaceDemo

Model-View-ViewModelSrovnání s MVCBindingDemo

dajbych.net

@Dajbych

feed.dajbych.net

events.dajbych.net

© 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.