02 | Beginning Code First Adam Tuliper | Technical Evangelist

Slides:



Advertisements
Similar presentations
1 SQL Server Management Studio SQL DDL CREATE TABLE Constraints ALTER TABLE DROP TABLE The GUI way Steen Jensen, autumn 2013.
Advertisements

.NET Database Technologies: Open-Source Frameworks.
Telerik Software Academy Telerik School Academy.
Designing a Database Unleashing the Power of Relational Database Design.
Virtual techdays INDIA │ 9-11 February 2011 Entity Framework – Future of Middle Tier Design Sunil Rathi │ Technical Architect, Dell Services.
ORM Technologies and Entity Framework (EF)
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Configuring Sakai from Outside the Web Application Tony Atkins.
Neo.NET Entity Objects Design Goals Copyright © Erik Dörnenburg – Last updated: May 2004.
Introduction to MVC Adding Model Classes NTPCUG Tom Perkins, Ph.D.
Connecting a.NET application to a database Jim Warren, COMPSCI 280 S Enterprise Software Development.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
OVERVIEW ON HOW ENTITY FRAMEWORK CODE FIRST CAN EASE THE DEVELOPMENT PROCESS Entity Framework Code First 11/19/2013 Joe Walling Copyright Walling Info.
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Principal Consultant.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
Data Modeling Creating E/R Diagrams SoftUni Team Technical Trainers Software University
Domain-Driven Design for the Database-Driven Mind
SQL Server 2005: Extending the Type System with XML.
Simplifying the Code First Approach in the Entity Framework Dhananjay Kumar Infragistics Consultant Microsoft MVP
Jennifer Widom Relational Databases The Relational Model.
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
Standards and Conventions
Top 10 Entity Framework Features Every Developer Should Know
Working with Fluent API Inheritance Strategies
Introduction to Entity framework
Building Web Applications with Microsoft ASP
Introduction to .NET Florin Olariu
Introduction to Entity Framework
EF Code First (Advanced)
EF Advanced Querying Optimize Performance SoftUni Team Advanced
EF Relations Object Composition
Entity Framework: Code First
Entity Framework DB From Code, OOP Introduction
Entity Framework: Relations
C#: ASP.NET MVC Overview
Advanced Java Programming
Data Definition and Data Types
08 | Next Steps Jon Galloway | Technical Evangelist
Entity Framework By: Casey Griffin.
Entity Framework Core for Enterprise Applications
UML to XSD.
Learn. Imagine. Build. .NET Conf
Social Media And Global Computing Managing MVC with Model Validation
SharePoint Cloud hosted Apps
04 | Customizing Controllers
ADO.NET Entity Framework Marcus Tillett
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Deriving a Relational DB Model from the AIXM CM
JSON for the Data Mortal
Christopher Thielen, Lead Application Developer, DSS IT
IT College 2016, Andres käver
Entity Framework Core for Enterprise Applications
C# - EF Core IT College, Andres Käver, , Fall semester
Object Relational Mapping Tools
Implementing Entity Framework with MVC Jump Start
C# - EF Core Intro IT College, Andres Käver, , Fall semester
SQL – Application Persistence Design Patterns
06 | Integrating extra features and looking forward
SQL AUTO INCREMENT Field
Presentation transcript:

02 | Beginning Code First Adam Tuliper | Technical Evangelist Christopher Harrison | Content Developer

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

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 best practices 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

Strings Nullable nvarchar(max) is the default Attributes StringLengthAttribute MaximumLength MinimumLength RequiredAttribute

Numbers SQL data type is mapped to .NET data type RangeAttribute long becomes BigInt RangeAttribute Maximum Minimum

Numbers and nullability Numbers are value types in .NET .NET property must be marked as nullable Nullable<T> type?

Attributes and database control

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

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

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

Initializing the database