Entity Framework Core for Enterprise Applications

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements


Software Testing with Visual Studio 2013 & Team Foundation Server 2013 Benjamin Day.
(code name: Data Dude) Josh Robinson Aculix.
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
Entity Framework Code First End to End
@benday #vslive Automated Build, Test & Deploy with TFS, ASP.NET, and SQL Server Benjamin
Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during WakeUpAndCode.com.
Windows Azure Tour Benjamin Day Benjamin Day Consulting, Inc.
Building an Offline Smart Client using Domain-Driven Design Principles Tim McCarthy.
Top 10 Ways to Go from Good to Great Scrum Master Benjamin Day.
Team Foundation Server 2012 Builds: Understand, Configure, and Customize Benjamin Day.
Ronnie Saurenmann Principal Architect Microsoft Switzerland.
How to be a C# ninja in 10 easy steps Benjamin Day.
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Principal Consultant.
Database Projects in Visual Studio Improving Reliability & Productivity.
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Magenic, Principal Consultant Level: Introductory.
Entity Framework 7 Who Are You & What Have You Done to my ORM?
Real World SQL Server Data Tools Benjamin
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Developing SQL/Server database in Visual Studio Introducing SQL /Server Data Tools Peter Lu.Net Practices Director Principle Architect Nexient March 19.
Benjamin Day Get Good at DevOps: Feature Flag Deployments with ASP.NET, WebAPI, & JavaScript.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Benjamin Day Role-based Security Stinks: Better Authorization in ASP.NET.
Benjamin Day Real World Scrum with TFS 2015 & VSTS.
Top 10 Entity Framework Features Every Developer Should Know
Introduction ITEC 420.
DevOps with ASP.NET Core and Entity Framework Core
Introduction to Entity framework
Stephen W. Thomas Integration MVP
Building Web Applications with Microsoft ASP
Stress Free Deployments with Octopus Deploy
Introduction to .NET Florin Olariu
Introduction to Entity Framework
Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
5/15/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
EF Relations Object Composition
Entity Framework: Code First
What’s New in SQL Server 2016 Master Data Services
 .NET CORE
Did your feature got in, out or planned?
Entity Framework By: Casey Griffin.
Entity Framework Core for Enterprise Applications
Unit Testing & Test-Driven Development for Mere Mortals
Learn. Imagine. Build. .NET Conf
ADO.NET Entity Framework Marcus Tillett
ADO.NET Entity Framework
Unit Testing & Test-Driven Development for Mere Mortals
Microsoft Build /15/2018 6:28 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
An Introduction to Entity Framework
Get Good at DevOps: Feature Flag Deployments with ASP
Entity Framework Core.
Entity Framework Core (EF Core)
DAT381 Team Development with SQL Server 2005
Unit Testing & Test-Driven Development for Mere Mortals
Real World Scrum with TFS & VSTS / Azure DevOps
Your code is not just…your code
Presented by : Chirag Dani & Dhaval Shah
SSDT and Database Project Basics
From Development to Production: Optimizing for Continuous Delivery
Non-Useless Unit Testing for Entity Framework Core & ASP.NET Core
Implementing Security in ASP.NET Core: Claims, Patterns, and Policies
C# - EF Core IT College, Andres Käver, , Fall semester
From Development to Production: Optimizing for Continuous Delivery
Implementing Entity Framework with MVC Jump Start
C# - EF Core Intro IT College, Andres Käver, , Fall semester
Visual Studio + SQL Server Is Better
SSDT, Docker, and (Azure) DevOps
Your code is not just…your code
Presentation transcript:

Entity Framework Core for Enterprise Applications Benjamin Day @benday

Benjamin Day Brookline, MA Consultant & Trainer Rental Chief Technology Officer Therapist for Teams Scrum, DevOps, Azure, Team Foundation Server, Software Architecture & Testing Microsoft MVP Pluralsight Author Scrum.org Trainer @benday

Getting Started with Visual Studio Team Services (VSTS)

Architecting an ASP.NET Core MVC Application for Unit Testability Coming soon!

On with the show.

Overview EF Core vs. EF Standard Relationships Lazy Loading Property & Navigation Types Querying with Shadow Properties The Dreaded Many-to-Many Lazy Loading Cascading Deletes Concurrency Management Software Architecture Unit Testing Schema Management DevOps Performance in a Web App

How did we get here?

ADO.NET

Object-Relational Impedance Mismatch, Object-Relational Mappers (ORMs)

Anything that lets me go fast now, robs me of my ability to maintain it later.

Enterprise Software = Non-disposable software

Rule #1: You don’t work for EF. EF works for you.

Rule #2: Use EF for what it’s good at and be ready to bail out when it doesn’t.

Rule #3: Hide EF from your application.

Rule #4: Just because you can doesn’t mean you should.

.NET Core 2.2 & EF Core 2.2 released this week

EF Core vs. EF Standard Full feature comparison It’s mostly the same https://docs.microsoft.com/en-us/ef/efcore-and-ef6/index It’s mostly the same If you’re “code first” Some things are missing

Big things that are missing (~2017 Edition) Complex types without identity Name, PhoneNumber Many-to-many relationships without a JOIN entity Inheritance mapping only does Table-per-hierarchy (TPH) No Table-per-type (TPT) Spatial types Geography & Geometry Saving using Stored Procedures (Easily at least) Raw SQL to anonymous types Lazy loading

Big things that are missing (2018 Edition) Complex types without identity Name, PhoneNumber Many-to-many relationships without a JOIN entity Inheritance mapping only does Table-per-hierarchy (TPH) No Table-per-type (TPT) Spatial types Geography & Geometry Saving using Stored Procedures (Easily at least) Raw SQL to anonymous types Lazy loading

Lazy Loading

Lazy Loading Person has PhoneNumbers Phone has Owner public IList<Phone> PhoneNumbers { get; } Phone has Owner public Person Owner { get; set; } In EF Standard  PhoneNumbers loaded when accessed In EF Core  PhoneNumbers is null (by default)

“Lazy” Loading Options Explicit Loading Include() ThenInclude() Lazy Loading (Easy) DbContextBuilder UseLazyLoadingProxies() Mark ‘lazy’ properties as virtual Lazy Loading (Harder) ILazyLoader on Entity’s constructor Custom logic

Demo: Person & PhoneNumbers

Owned Types

Owned Types Classes that are mapped but do not have identity How to use them? Create a property on an entity that is an instance of an owned type On the owned type, mark it with [Owned] Enjoy

Demo: Name as an Owned Type

Relationships

Relationships Principal Entity Dependent Entity One to One One to Many Terminology Relationship Types Principal Entity Owns the primary key Parent Dependent Entity Has foreign key Child in relationship One to One Navigation property One to Many Collection navigation property Many to One Inverse navigation property

Cascading Deletes What happens when something in a relationship is deleted? Delete Types Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior Cascade  Delete the dependent entity SetNull  Delete Principal, set inverse property on Dependent to null Restrict  Do nothing, delete is manual

Demo: Animals & Foods

Concurrency Management

Concurrency Management Multiple people using your app without stomping on each other’s data Configure using Fluent API or Attribute [Timestamp] attribute byte[] [ConcurrencyCheck] attribute DateTime, string, or int

Stored Procedures

Stored Procedures from EF Core Better for retrieves than for saves Single result set Only for mapped types (not anonymous) Has to bring back all properties/fields

Demo: Animals By Food Id

Recommended Software Architecture

Architecture Recommendations Don’t try to model inheritance in EF Hide EF behind Repository Pattern & Adapter Pattern Code to interfaces Treat EF entities as data access objects Hide your data access objects Write unit tests Unit tests are not Integration tests Write integration tests for the Repositories Test your Collection saves & deletes Test your concurrency

Performance Tips

Performance Tips Only optimize for known performance problems! TagWith(string) Adds a comment to the generated T-SQL If you don’t need to save it in the same transaction… AsNoTracking() Turns off change tracking Think hard about eager loading Minimize your use of Include(), ThenInclude() Only bring back what you need

DevOps & Schema Management

Humans are terrible at multitasking.

Productivity vs. Waste "Quality Software Management: Vol. 1 System Thinking“, Gerald Weinberg (1992)

To be productive, eliminate distractions.

Software delivery not software development.

Anything that slows you down, automate it.

What is DevOps?

DevOps is a mindset plus a set of practices that focuses on automation.

Why DevOps? Minimize distractions Focus on delivering software Eliminate the tedious stuff Bridge the divide between development, delivery, deployment, and operations

Make delivery & deployment easy.

If delivery & deployment is easy… …it’s not a big deal …you’ll do it more often …you can focus more time writing & delivering features

DevOps Goals Repeatable, Automated, & Version Controlled Including configuration Never deploy from a developer workstation No right-click  deploy Deploy from TFS Build Deploy from TFS Release Management Pipeline

Everything happens through the “dotnet” command.

DevOps: Don’t Forget the Database! Does your app use a database? You’ll need to version control it Automated build & deploy of database schemas Two approaches: SQL Server Data Tools (SSDT) Entity Framework Migrations

.NET Core DevOps Tip: Learn how to do everything from the command line

Entity Framework Core

EF Core NuGet Packages Talk to SQL Server Microsoft.EntityFrameworkCore.SqlServer Database Updates (“Migrations”) Microsoft.EntityFrameworkCore.Design Reverse Engineer a SQL Server Database Microsoft.EntityFrameworkCore.SqlServer.Design

EF Migrations = Entity Framework Version Control

Migrations from the Command Line Add / Remove a Migration dotnet ef migrations add “{name}” dotnet ef migrations remove Deploy an Update dotnet ef database update

Demo: Migrations Add an Initial Migration Deploy the Database DbContextFactory Fix hard-coded connection strings

Import Existing Database to EF Core dotnet ef dbcontext scaffold dotnet ef dbcontext scaffold -c {dbcontext-name} "Server=(local); Database={database}; Trusted_Connection=true;" Microsoft.EntityFrameworkCore.SqlServer

Team Foundation Server 2017 & VSTS Demos

Demo: EF Core with TFS Build Create a basic build Edit connection strings Deploy db changes using EF Migrations

Demo: EF Core with TFS Release Management Use Release Management Deploy to Multiple Environments Add Environment Approvals

Any last questions?

Thank you. www.benday.com | benday@benday.com