02 - Beginning Code First.pptx

Embed Size (px)

Citation preview

  • 7/24/2019 02 - Beginning Code First.pptx

    1/24

    Click toMaster

    style

    02 | Beginning Code First

    Adam Tuliper | Technical Evangelist

    Christopher Harrison | Content Developer

  • 7/24/2019 02 - Beginning Code First.pptx

    2/24

    Simple Code First

    Creating classes

    Creating Data Context

    nitiali!ing the data"ase

    Code First

  • 7/24/2019 02 - Beginning Code First.pptx

    3/24

    Click toMaster style

    Simple Code First

  • 7/24/2019 02 - Beginning Code First.pptx

    4/24

    publicclassArtist{ publicintArtistID { get;set; }

    publicstringName { get; s} publicstringBio { get;set; }

    }

  • 7/24/2019 02 - Beginning Code First.pptx

    5/24

    Code #rst classes$

    are %ust classes

  • 7/24/2019 02 - Beginning Code First.pptx

    6/24

    Code #rst class design tips

    &ust design 'our classes the (a' 'ou t'pica )*or the most part+

    ,se "est practices D *or the D

    -ou can still control the data"ase Attri"utes Fluent A.

  • 7/24/2019 02 - Beginning Code First.pptx

    7/24

    Click toMaster style

    Creating classes

  • 7/24/2019 02 - Beginning Code First.pptx

    8/24

    But (hat a"out m' data"ase/

    Code First o*ten does the right thing

    But it does need a little guidance

    For example strings "ecome nvarchar)max

  • 7/24/2019 02 - Beginning Code First.pptx

    9/24

    Basic data"ase control

    Data annotations can "e used to provide ad

    context S'stem1Componentodel1DataAnnotations https344msdn1microso*t1com4en5us4li"rar'4s'stem

    ntmodel1dataannotations)v6vs1770+1aspx

    8ot speci#c to Entit' Frame(or9 ,sed "' several other plat*orms including :C

    https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx
  • 7/24/2019 02 - Beginning Code First.pptx

    10/24

    Code First conventions

    Ta"les are automaticall' plurali!ed

    Ta"les are created in the d"o schema

    D propert' is created as the primar' 9e' dentit' or auto5count column

  • 7/24/2019 02 - Beginning Code First.pptx

    11/24

    Ta"le creation

    Ta"leAttri"ute

    Schema 8ame

    ColumnAttri"ute 8ame

  • 7/24/2019 02 - Beginning Code First.pptx

    12/24

    Strings

    8ulla"le nvarchar)max+ is the de*ault

    Attri"utes String;engthAttri"ute

    aximum;ength

    inimum;ength

  • 7/24/2019 02 - Beginning Code First.pptx

    13/24

    8um"ers

    S>; data t'pe is mapped to 18ET data t'pe

    long "ecomes Bignt

  • 7/24/2019 02 - Beginning Code First.pptx

    14/24

    8um"ers and nulla"ilit'

    8um"ers are value t'pes in 18ET

    18ET propert' must "e mar9ed as nulla"le 8ulla"le?T@ t'pe/

  • 7/24/2019 02 - Beginning Code First.pptx

    15/24

    DEAttri"utes and data"ase control

  • 7/24/2019 02 - Beginning Code First.pptx

    16/24

    Click toMaster style

    Creating the Data Context

  • 7/24/2019 02 - Beginning Code First.pptx

    17/24

    Creating the Data Context

    &ust li9e creating a normal class

    ts li9e its called Code First

    publicclassMusicStoreDbContext: DbC

    { publicDbSet Artists { get;

    }

  • 7/24/2019 02 - Beginning Code First.pptx

    18/24

    DECreating the D"Context

  • 7/24/2019 02 - Beginning Code First.pptx

    19/24

    The Find)+ method

  • 7/24/2019 02 - Beginning Code First.pptx

    20/24

    DECreating a repositor'

  • 7/24/2019 02 - Beginning Code First.pptx

    21/24

    Click toMaster style

    nitiali!ing the data"ase

  • 7/24/2019 02 - Beginning Code First.pptx

    22/24

    Testing re=uires a 9no(n starting

    Entit' Frame(or9 provides data"ase initiali!

    create that state

    Create a class that inherits *rom the appropoption CreateData"ase*8otExists DropCreateData"asehenodelChanges DropCreateData"aseAl(a's

    verride the Seed method to create data"a

    content

  • 7/24/2019 02 - Beginning Code First.pptx

    23/24

    DEnitiali!ing the data"ase

  • 7/24/2019 02 - Beginning Code First.pptx

    24/24

    207 icroso*t Corporation1 All rights reserved1 icroso*t indo(s ce A!ure S'stem Center D'namics and other product names atrademar9s and4or trademar9s in the ,1S1 and4or other countries1 The in*ormation herein is *or in*ormational purposes onl' and representicroso*t Corporation as o* the date o* this presentation1 Because icroso*t must respond to changing mar9et conditions it should not "e

    commitment on the part o* icroso*t and icroso*t cannot guarantee the accurac' o* an' in*ormation provided a*ter the date o* this presAGES 8 A