02 beginning code first

24
Click to edit Master subtitle style 02 | Beginning Code First Adam Tuliper | Technical Evangelist Christopher Harrison | Content Developer
  • Upload

    -
  • Category

    Education

  • view

    1.601
  • download

    0

Transcript of 02 beginning code first

Page 1: 02   beginning code first

Click to edit Master subtitle style

02 | Beginning Code First

Adam Tuliper | Technical EvangelistChristopher Harrison | Content Developer

Page 2: 02   beginning code first

• Simple Code First• Creating classes• Creating Data Context• Initializing the database

Code First

Page 3: 02   beginning code first

Click to edit Master subtitle style

Simple Code First

Page 4: 02   beginning code first

public class Artist{ public int ArtistID { get; set; } public string Name { get; set; } public string Bio { get; set; }}

Page 5: 02   beginning code first

Code first classes…

are just classes

Page 6: 02   beginning code first

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

Page 7: 02   beginning code first

Click to edit Master subtitle style

Creating classes

Page 8: 02   beginning code first

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)

Page 9: 02   beginning code first

Basic database control• Data annotations can be used to provide additional

context– System.ComponentModel.DataAnnotations– https://msdn.microsoft.com/en-us/library/system.compone

ntmodel.dataannotations(v=vs.110).aspx

• Not specific to Entity Framework– Used by several other platforms, including MVC

Page 10: 02   beginning code first

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

Page 11: 02   beginning code first

Table creation• TableAttribute– Schema– Name

• ColumnAttribute– Name

Page 12: 02   beginning code first

Strings• Nullable nvarchar(max) is the default

• Attributes– StringLengthAttribute• MaximumLength• MinimumLength

– RequiredAttribute

Page 13: 02   beginning code first

Numbers• SQL data type is mapped to .NET data type– long becomes BigInt

• RangeAttribute–Maximum–Minimum

Page 14: 02   beginning code first

Value types and nullability• Dates and numbers are value types in .NET

• Value type properties must be marked as nullable– Nullable<T>– type?

Page 15: 02   beginning code first

DEMOAttributes and database control(warning... This demo is going to fail. Well, sort of.)

Page 16: 02   beginning code first

Click to edit Master subtitle style

Creating the Data Context

Page 17: 02   beginning code first

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; }}

Page 18: 02   beginning code first

DEMOCreating the DbContext

Page 19: 02   beginning code first

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

Page 20: 02   beginning code first

DEMOCreating a repository

Page 21: 02   beginning code first

Click to edit Master subtitle style

Initializing the database

Page 22: 02   beginning code first

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

Page 23: 02   beginning code first

DEMOInitializing the database

Page 24: 02   beginning code first

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