02 beginning code first
TRANSCRIPT
![Page 1: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/1.jpg)
Click to edit Master subtitle style
02 | Beginning Code First
Adam Tuliper | Technical EvangelistChristopher Harrison | Content Developer
![Page 2: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/2.jpg)
• Simple Code First• Creating classes• Creating Data Context• Initializing the database
Code First
![Page 3: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/3.jpg)
Click to edit Master subtitle style
Simple Code First
![Page 4: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/4.jpg)
public class Artist{ public int ArtistID { get; set; } public string Name { get; set; } public string Bio { get; set; }}
![Page 5: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/5.jpg)
Code first classes…
are just classes
![Page 6: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/7.jpg)
Click to edit Master subtitle style
Creating classes
![Page 8: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/11.jpg)
Table creation• TableAttribute– Schema– Name
• ColumnAttribute– Name
![Page 12: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/12.jpg)
Strings• Nullable nvarchar(max) is the default
• Attributes– StringLengthAttribute• MaximumLength• MinimumLength
– RequiredAttribute
![Page 13: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/13.jpg)
Numbers• SQL data type is mapped to .NET data type– long becomes BigInt
• RangeAttribute–Maximum–Minimum
![Page 14: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/15.jpg)
DEMOAttributes and database control(warning... This demo is going to fail. Well, sort of.)
![Page 16: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/16.jpg)
Click to edit Master subtitle style
Creating the Data Context
![Page 17: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/18.jpg)
DEMOCreating the DbContext
![Page 19: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/20.jpg)
DEMOCreating a repository
![Page 21: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/21.jpg)
Click to edit Master subtitle style
Initializing the database
![Page 22: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/23.jpg)
DEMOInitializing the database
![Page 24: 02 beginning code first](https://reader035.vdocuments.us/reader035/viewer/2022062502/58a63cb91a28ab68118b616b/html5/thumbnails/24.jpg)
©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.