Working with Fluent API Inheritance Strategies

Slides:



Advertisements
Similar presentations
Entity Framework Performance SoftUni Team Technical Trainers Software University
Advertisements

ORM Technologies and Entity Framework (EF) ORM Concepts, Entity Framework, DbContext, CRUD Operations 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
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Joins, Subqueries, CTE and Indices
Auto Mapping Objects SoftUni Team Database Applications
Introduction to Entity framework
Databases basics Course Introduction SoftUni Team Databases basics
C# Basic Syntax, Visual Studio, Console Input / Output
Interface Segregation / Dependency Inversion
Introduction to MVC SoftUni Team Introduction to MVC
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
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
Database Design and Rules
EF Advanced Querying Optimize Performance SoftUni Team Advanced
EF Relations Object Composition
Entity Framework: Code First
Parsing XML XDocument and LINQ
Repeating Code Multiple Times
Entity Framework DB From Code, OOP Introduction
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
Abstraction, Interface, Inheritance, Polymorphism, Override / Overload
Install and configure theme
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Fast String Manipulation
Functional Programming
The Right Way Control Flow
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
Transactions in Entity Framework
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best Practices and Architecture
Best practices and architecture
Introduction to Databases
WordPress Plugins Popular WP Plugins: Sliders, Forms, Contacts, SEO, Forum, Photo Gallery, e-Commerce WordPress Plugins SoftUni Team Technical Trainers.
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
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
Iterators and Comparators
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Hibernate (JPA) Code First Entity Relations
Spring Data Advanced Querying
Entity Framework Advanced Querying SoftUni Team Technical Trainers
Polymorphism, Interfaces, Abstract Classes
JavaScript: ExpressJS Overview
CSS Transitions and Animations
Presentation transcript:

Working with Fluent API Inheritance Strategies Advanced EF Relations Working with Fluent API Inheritance Strategies Advanced Relations 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 Fluent API (Model Builder) Joining and Grouping Tables View Models Inheritance Strategies © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

sli.do #Entity Questions © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Working with Model Builder Fluent API Working with Model Builder

Fluent API Code First maps your POCO classes to tables using a set of conventions E.g. property named "Id" maps to the Primary Key Can be customized using annotations and the Fluent API Fluent API (Model Builder) allows full control over DB mappings Custom names of objects (columns, tables, etc.) in the DB Validation and data types Fix complicated entity relationships

Resume default behavior Initialize Fluent API Custom mappings are placed inside the OnModelCreating method of the context protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Student>().HasKey(s => s.StudentKey); base.OnModelCreating(modelBuilder); } Resume default behavior

Rename DB Objects Custom Table name Custom Column name Optional schema name modelBuilder.Entity<Order>().ToTable("OrderRef", "Admin"); modelBuilder.Entity<Student>() .Property(s => s.Name) .HasColumnName("StudentName") .HasColumnType("varchar"); Optional data type

Column Attributes Explicitly set Primary Key Other column attributes modelBuilder.Entity<Student>().HasKey("StudentKey"); modelBuilder.Entity<Person>().Property(p => p.FirstName) .IsOptional() .IsRequired() .IsFixedLength() .HasMaxLength()

Address contains FK to Student Entity Relationships One-to-Zero-or-One One-to-One Address contains FK to Student modelBuilder.Entity<Address>() .HasRequired(a => a.Student) .WithOptional(s => s.Address); modelBuilder.Entity<Address>() .HasRequired(a => a.Student) .WithRequiredDependent(s => s.Address);

Entity Relationships (2) One-to-Many One-to-Many with custom FK name modelBuilder.Entity<Comment>() .HasRequired(c => c.Post) .WithMany(p => p.Comments) modelBuilder.Entity<Comment>() .HasRequired(c => c.Post) .WithMany(p => p.Comments) .HasForeignKey(c => c.PostKey);

Entity Relationships (3) Many-to-Many Many-to-Many with custom FK names modelBuilder.Entity<Student>() .HasMany(s => s.Courses) .WithMany(c => c.Students) … .Map(cs => { cs.ToTable("StudentCourses"); cs.MapLeftKey("StudentKey"); cs.MapRightKey("CourseRefId"); });

Other Options Do not include property in DB Disabling cascade delete If a FK property is non-nullable, cascade delete is on by default modelBuilder.Entity<Department>().Ignore(d => d.Budget); modelBuilder.Entity<Course>() .HasRequired(t => t.Department) .WithMany(t => t.Courses) .HasForeignKey(d => d.DepartmentID) .WillCascadeOnDelete(false);

Specialized Configuration Classes Mappings can be placed in entity-specific classes Include in OnModelCreating: public class StudentConfiguration : EntityTypeConfiguration<Student> { public StudentConfiguration() this.HasKey(s => s.StudentKey); } Specify target model No need to reference modelBuilder modelBuilder.Configurations.Add(new StudentConfiguration());

Filtering and Aggregating Tables Select, Join and Group Data Using LINQ

Why use select Limit network traffic by reducing the queried columns Syntax: var employeesWithTown = context .Employees .Select(employee => new { EmployeeName = employee.Name, TownName = employee.Town.Name });

Why not to use select Data that is selected is not of the initial entity type, but of an anonymous type that is generated runtime (a bit more expensive) Data cannot be modified (updated, deleted), when selected, because we are not working with the actual object, but with a read-only “copy” of it © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Joining Tables in EF Join tables in EF with LINQ / extension methods on IEnumerable<T> (like when joining collections) var employees = from employee in softUniEntities.Employees join department in softUniEntities.Departments on employee.EmployeeID equals department.DepartmentID select new { Employee = employee.FirstName, JobTitle = employee.JobTitle, Department = department.Name }; var employees = softUniEntities.Employees.Join( softUniEntities.Departments, (e => e.DepartmentID), (d => d.DepartmentID), (e, d) => new { Employee = e.FirstName, JobTitle = e.JobTitle, Department = d.Name } );

Grouping Tables in EF Grouping also can be done by LINQ The same way as with collections in LINQ Grouping with LINQ: Grouping with extension methods: var groupedEmployees = from employee in softUniEntities.Employees group employee by employee.JobTitle; var groupedCustomers = softUniEntities.Employees .GroupBy(employee => employee.JobTitle);

ViewModels Select, Group, Join can work with custom classes Allows you to pass them to methods and use them as return type Requires some extra code (class definition) Sample ViewModel: public class UserInfoView { public string Alias { get; set; } public byte[] Avatar { get; set; } }

View Models (2) Assign the fields as you would with an anonymous object: The new type can be used in a method signature: var currentUser = context.Users .Find(8) .Select(u => new UserInfoView { Alias = u.FirstName + " " + u.LastName, Avatar = u.Avatar }) .SingleOrDefault(); public static UserInfoView GetUserInfo(int Id) { … }

Inheritance Strategies Vehicle Car Truck Electric Petrol Inheritance Strategies Mapping Class Hierarchies to DB Objects © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Object Inheritance Extending system classes Extending C# classes Extending Domain Models (POCOs)

Table per Hierarchy (TPH) One table for all classes in the chain Differentiated by Discriminator column modelBuilder.Entity<Vehicle>() .Map<Car>(m => m .Requires(“Discriminator") .HasValue(“Car”) ) .Map<Truck>(m => m .Requires("Discriminator") .HasValue(“Truck”) );

Table per Type (TPT) One table for each class, containing only the needed properties Parent-class table contains all common properties Child-class tables have a shared PK with parent-class table modelBuilder.Entity<ChildClassOne>() .ToTable(“TableNameOfChildClassOne”); modelBuilder.Entity<ChildClassTwo>() .ToTable(“TableNameOfChildClassTwo”);

Table per Concrete Type (TPC) Each entity has it's own table with no obvious relation The base domain model must be modified: modelBuilder.Entity<Car>().Map(m => { m.MapInheritedProperties(); m.ToTable("Car"); }); modelBuilder.Entity<ParentClass>() .Property(p => p.”Id”) .HasDatabaseGenerationOption(DatabaseGenerationOption.None);

Summary The Fluent API gives us full control over Entity Framework object mappings Information overhead can be limited by selecting only the needed properties ModelViews can be used to move aggregated data between methods Different Inheritance Strategies can be used for optimal DB performance © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Entity Framework Relations 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 Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.