Entity Framework: Code First

Slides:



Advertisements
Similar presentations
ORM Technologies and Entity Framework (EF)
Advertisements

NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
OVERVIEW ON HOW ENTITY FRAMEWORK CODE FIRST CAN EASE THE DEVELOPMENT PROCESS Entity Framework Code First 11/19/2013 Joe Walling Copyright Walling Info.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Entity Framework: Code First SoftUni Team Technical Trainers Software University
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Version Control Systems
Auto Mapping Objects SoftUni Team Database Applications
Working with Fluent API Inheritance Strategies
Introduction to Entity framework
Databases basics Course Introduction SoftUni Team Databases basics
Interface Segregation / Dependency Inversion
Services & Dependency Injection
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
C# Database Fundamentals with Microsoft SQL Server
Introduction to Entity Framework
ASP.NET Integration Testing
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
Classes, Properties, Constructors, Objects, Namespaces
Mocking tools for easier unit testing
Parsing JSON JSON.NET, LINQ-to-JSON
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
EF Advanced Querying Optimize Performance SoftUni Team Advanced
EF Relations Object Composition
Parsing XML XDocument and LINQ
Entity Framework DB From Code, OOP Introduction
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
Install and configure theme
Entity Framework: Relations
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
Transactions in Entity Framework
ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best Practices and Architecture
Best practices and architecture
Magento Basics part 2 Modules & Themes Stenik Group Ltd. Magento
Data Definition and Data Types
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
ASP.NET REST Services SoftUni Team ASP.NET REST Services
Exporting and Importing Data
ASP.NET Filters SoftUni Team ASP.NET MVC Introduction
Making big SPA applications
Manual Mapping and AutoMapper Library
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Hibernate (JPA) Code First Entity Relations
Spring Data Advanced Querying
Software Quality Assurance
Version Control Systems
Lean .NET stack for building modern web apps
JavaScript: ExpressJS Overview
CSS Transitions and Animations
IT College 2016, Andres käver
Presentation transcript:

Entity Framework: Code First Database Applications SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents EF Modelling Workflows Code First Main Parts Domain Classes (Models) DbContext and DbSet Database connection Using Code First Migrations Configuring Mappings © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Questions sli.do #Entity

EF supports two modeling workflows with code first. Code first from database Empty code first model

Why Use Code First? Write code without having to define mappings in XML or create database models Define objects in C# format Reuse these models and their attributes Enables database persistence with no configuration Can use automatic migrations Can use data annotations (Key, Required, etc.)

Domain Classes, DbContext and DbSet Main Parts Domain Classes, DbContext and DbSet

Creating Domain Classes (Models)

Creating DbContext

CRUD Operations with EF Code First

Change Database Connection

Using Code EF First Migrations

Code First Migrations Enable Code First Migrations Open Package Manager Console Run Enable-Migrations command This will create some initial jumpstart code -EnableAutomaticMigrations for auto migrations Two types of migrations Automatic migrations Set AutomaticMigrationsEnabled = true; Code-based (providing full control) Separate C# code file for every migration

Database Migration Strategies CreateDatabaseIfNotExists Default migration DropCreateDatabaseIfModelChanges We lose all the data when the model changes DropCreateDatabaseAlways Great for automated integration testing MigrateDatabaseToLatestVersion This option uses our migrations Custom migrations Implement IDatabaseInitializer for custom migration strategy

Using Data Annotations and EF Fluent API Configure Mappings Using Data Annotations and EF Fluent API

Configure Mappings Entity Framework respects mapping details from two sources: Data annotation attributes in the models E.g. [Key], [Required], [MaxLength] Can be reused for validation purposes Fluent API code mapping configuration By overriding OnModelCreating method By using custom configuration classes Use one approach or the other

Data Annotations There is a bunch of data annotation attributes System.ComponentModel.DataAnnotations Specify the primary key of the table: [Key] For validation: [StringLength], [MaxLength], [MinLength], [Required], [Range] Schema: [Column], [Table], [ComplexType], [Timestamp], [NotMapped], [Index] EF 6 supports custom attributes by using custom conventions

Entity Framework Fluent API Using the ModelBuilder

EF Fluent API Configurations .Entity() Map: Table Name, Schema Inheritance Hierarchies, Complex Types Entity -> Multiple Tables Table -> Multiple Entities Specify Key (including Composite Keys) .Property() Attributes (and Validation) Map: Column Name, Type, Order Relationships Concurrency

https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.