02 | Beginning Code First
Adam Tuliper | Technical Evangelist
Christopher Harrison | Content Developer
• Simple Code First
• Creating classes
• Creating Data Context
• Initializing the database
Code First
Simple Code First
public class Artist
{
public int ArtistID { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
}
Code first classes…
are just classes
Code first class design tips
• Just design your classes the way you typically would
– (for the most part)
• Use standard conventions
– ID for the ID
• You can still control the database
– Attributes
– Fluent API
Creating classes
But what about my database?
• Code First often does the right thing
• But it does need a little guidance
• For example, strings become nvarchar(max)
Basic database control
• Data annotations can be used to provide additional context
– System.ComponentModel.DataAnnotations
– https://msdn.microsoft.com/en-
us/library/system.componentmodel.dataannotations(v=vs.110).aspx
• Not specific to Entity Framework
– Used by several other platforms, including MVC
Code First conventions
• Tables are automatically pluralized
• Tables are created in the dbo schema
• ID property is created as the primary key
– Identity or auto-count column
Table creation
• TableAttribute
– Schema
– Name
• ColumnAttribute
– Name
Strings
• Nullable nvarchar(max) is the default
• Attributes
– StringLengthAttribute
• MaximumLength
• MinimumLength
– RequiredAttribute
Numbers
• SQL data type is mapped to .NET data type
– long becomes BigInt
• RangeAttribute
– Maximum
– Minimum
Value types and nullability
• Dates and numbers are value types in .NET
• Value type properties must be marked as nullable
– Nullable<T>
– type?
DEMO
Attributes and database control
(warning... This demo is going to fail. Well, sort of.)
Creating the Data Context
Creating the Data Context
• Just like creating a normal class
– It's like it's called Code First 
public class MusicStoreDbContext : DbContext
{
public DbSet<Artist> Artists { get; set; }
}
DEMO
Creating the DbContext
The Find() method
• Regardless of the data type you're going to need to look the
object up by its key
• Rewriting that code over and over again becomes tedious
• The Find method will do that for you
• Accepts a parameter that maps to the key
• Returns the object if it's found
• Returns null if it isn't
DEMO
Creating a repository
Initializing the database
Testing requires a known starting state
• Entity Framework provides database initializers to create that
state
• Create a class that inherits from the appropriate option
– CreateDatabaseIfNotExists
– DropCreateDatabaseWhenModelChanges
– DropCreateDatabaseAlways
• Override the Seed method to create database content
• Register the method with Database.SetInitializer
DEMO
Initializing the database
©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics 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.

02 beginning code first

  • 1.
    02 | BeginningCode First Adam Tuliper | Technical Evangelist Christopher Harrison | Content Developer
  • 2.
    • Simple CodeFirst • Creating classes • Creating Data Context • Initializing the database Code First
  • 3.
  • 4.
    public class Artist { publicint ArtistID { get; set; } public string Name { get; set; } public string Bio { get; set; } }
  • 5.
  • 6.
    Code first classdesign tips • Just design your classes the way you typically would – (for the most part) • Use standard conventions – ID for the ID • You can still control the database – Attributes – Fluent API
  • 7.
  • 8.
    But what aboutmy database? • Code First often does the right thing • But it does need a little guidance • For example, strings become nvarchar(max)
  • 9.
    Basic database control •Data annotations can be used to provide additional context – System.ComponentModel.DataAnnotations – https://msdn.microsoft.com/en- us/library/system.componentmodel.dataannotations(v=vs.110).aspx • Not specific to Entity Framework – Used by several other platforms, including MVC
  • 10.
    Code First conventions •Tables are automatically pluralized • Tables are created in the dbo schema • ID property is created as the primary key – Identity or auto-count column
  • 11.
    Table creation • TableAttribute –Schema – Name • ColumnAttribute – Name
  • 12.
    Strings • Nullable nvarchar(max)is the default • Attributes – StringLengthAttribute • MaximumLength • MinimumLength – RequiredAttribute
  • 13.
    Numbers • SQL datatype is mapped to .NET data type – long becomes BigInt • RangeAttribute – Maximum – Minimum
  • 14.
    Value types andnullability • Dates and numbers are value types in .NET • Value type properties must be marked as nullable – Nullable<T> – type?
  • 15.
    DEMO Attributes and databasecontrol (warning... This demo is going to fail. Well, sort of.)
  • 16.
  • 17.
    Creating the DataContext • Just like creating a normal class – It's like it's called Code First  public class MusicStoreDbContext : DbContext { public DbSet<Artist> Artists { get; set; } }
  • 18.
  • 19.
    The Find() method •Regardless of the data type you're going to need to look the object up by its key • Rewriting that code over and over again becomes tedious • The Find method will do that for you • Accepts a parameter that maps to the key • Returns the object if it's found • Returns null if it isn't
  • 20.
  • 21.
  • 22.
    Testing requires aknown starting state • Entity Framework provides database initializers to create that state • Create a class that inherits from the appropriate option – CreateDatabaseIfNotExists – DropCreateDatabaseWhenModelChanges – DropCreateDatabaseAlways • Override the Seed method to create database content • Register the method with Database.SetInitializer
  • 23.
  • 24.
    ©2013 Microsoft Corporation.All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics 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.

Editor's Notes