EntityFrameworkNotesForProfessionals.pdf

(1512 KB) Pobierz
Entity
Framework
Entity Framework
Notes for Professionals
Notes for Professionals
of professional hints and tricks
80+ pages
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Entity Framework group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
................................................................................................................................................................................... 1
Chapter 1: Getting started with Entity Framework
..................................................................................... 2
Section 1.1: Installing the Entity Framework NuGet Package
.................................................................................... 2
Section 1.2: Using Entity Framework from C# (Code First)
....................................................................................... 4
Section 1.3: What is Entity Framework?
....................................................................................................................... 5
Chapter 2: Code First Conventions
...................................................................................................................... 6
Section 2.1: Removing Conventions
............................................................................................................................. 6
Section 2.2: Primary Key Convention
.......................................................................................................................... 6
Section 2.3: Type Discovery
......................................................................................................................................... 6
Section 2.4: DecimalPropertyConvention
................................................................................................................... 7
Section 2.5: Relationship Convention
.......................................................................................................................... 9
Section 2.6: Foreign Key Convention
........................................................................................................................ 10
Chapter 3: Optimization Techniques in EF
..................................................................................................... 11
Section 3.1: Using AsNoTracking
................................................................................................................................ 11
Section 3.2: Execute queries in the database when possible, not in memory
...................................................... 11
Section 3.3: Loading Only Required Data
................................................................................................................. 11
Section 3.4: Execute multiple queries async and in parallel
................................................................................... 12
Section 3.5: Working with stub entities
...................................................................................................................... 13
Section 3.6: Disable change tracking and proxy generation
.................................................................................. 14
Chapter 4: .t4 templates in entity framework
........................................................................................... 15
Section 4.1: Dynamically adding Interfaces to model
............................................................................................. 15
Section 4.2: Adding XML Documentation to Entity Classes
.................................................................................... 15
Chapter 5: Code First DataAnnotations
.......................................................................................................... 17
Section 5.1: [Column] attribute
................................................................................................................................... 17
Section 5.2: [DatabaseGenerated] attribute
............................................................................................................ 17
Section 5.3: [Required] attribute
................................................................................................................................ 18
Section 5.4: [MaxLength] and [MinLength] attributes
............................................................................................ 18
Section 5.5: [InverseProperty(string)] attribute
....................................................................................................... 19
Section 5.6: [ComplexType] attribute
....................................................................................................................... 20
Section 5.7: [ForeignKey(string)] attribute
............................................................................................................... 21
Section 5.8: [Range(min,max)] attribute
.................................................................................................................. 21
Section 5.9: [NotMapped] attribute
........................................................................................................................... 22
Section 5.10: [Table] attribute
.................................................................................................................................... 23
Section 5.11: [Index] attribute
..................................................................................................................................... 23
Section 5.12: [Key] attribute
....................................................................................................................................... 24
Section 5.13: [StringLength(int)] attribute
................................................................................................................. 25
Section 5.14: [Timestamp] attribute
.......................................................................................................................... 25
Section 5.15: [ConcurrencyCheck] Attribute
............................................................................................................. 26
Chapter 6: Database first model generation
............................................................................................... 27
Section 6.1: Generating model from database
......................................................................................................... 27
Section 6.2: Adding data annotations to the generated model
............................................................................. 28
Chapter 7: Model Restraints
................................................................................................................................. 30
Section 7.1: One-to-many relationships
.................................................................................................................... 30
Chapter 8: Code First - Fluent API
...................................................................................................................... 32
Section 8.1: Mapping models
...................................................................................................................................... 32
Section 8.2: Composite Primary Key
......................................................................................................................... 33
Section 8.3: Maximum Length
.................................................................................................................................... 34
Section 8.4: Primary Key
............................................................................................................................................ 34
Section 8.5: Required properties (NOT NULL)
......................................................................................................... 35
Section 8.6: Explict Foreign Key naming
................................................................................................................... 35
Chapter 9: Loading related entities
.................................................................................................................. 37
Section 9.1: Eager loading
.......................................................................................................................................... 37
Section 9.2: Explicit loading
........................................................................................................................................ 37
Section 9.3: Lazy loading
............................................................................................................................................ 38
Section 9.4: Projection Queries
.................................................................................................................................. 38
Chapter 10: Transactions
........................................................................................................................................ 40
Section 10.1: Database.BeginTransaction()
.............................................................................................................. 40
Chapter 11: Managing entity state
..................................................................................................................... 41
Section 11.1: Setting state Added of a single entity
................................................................................................... 41
Section 11.2: Setting state Added of an object graph
.............................................................................................. 41
Chapter 12: Entity Framework Code First
...................................................................................................... 43
Section 12.1: Connect to an existing database
.......................................................................................................... 43
Chapter 13: Database Initialisers
........................................................................................................................ 45
Section 13.1: CreateDatabaseIfNotExists
................................................................................................................... 45
Section 13.2: DropCreateDatabaseIfModelChanges
................................................................................................ 45
Section 13.3: DropCreateDatabaseAlways
............................................................................................................... 45
Section 13.4: Custom database initializer
.................................................................................................................. 45
Section 13.5: MigrateDatabaseToLatestVersion
...................................................................................................... 46
Chapter 14: Complex Types
................................................................................................................................... 47
Section 14.1: Code First Complex Types
.................................................................................................................... 47
Chapter 15: Tracking vs. No-Tracking
.............................................................................................................. 48
Section 15.1: No-tracking queries
............................................................................................................................... 48
Section 15.2: Tracking queries
.................................................................................................................................... 48
Section 15.3: Tracking and projections
...................................................................................................................... 48
Chapter 16: Entity framework Code First Migrations
............................................................................... 50
Section 16.1: Enable Migrations
................................................................................................................................... 50
Section 16.2: Add your first migration
....................................................................................................................... 50
Section 16.3: Doing "Update-Database" within your code
...................................................................................... 52
Section 16.4: Seeding Data during migrations
.......................................................................................................... 52
Section 16.5: Initial Entity Framework Code First Migration Step by Step
............................................................. 53
Section 16.6: Using Sql() during migrations
.............................................................................................................. 54
Chapter 17: Inheritance with EntityFramework (Code First)
................................................................ 56
Section 17.1: Table per hierarchy
................................................................................................................................ 56
Section 17.2: Table per type
....................................................................................................................................... 56
Chapter 18: Best Practices For Entity Framework (Simple & Professional)
................................. 58
Section 18.1: 1- Entity Framework @ Data layer (Basics)
........................................................................................ 58
Section 18.2: 2- Entity Framework @ Business layer
............................................................................................... 61
Section 18.3: 3- Using Business layer @ Presentation layer (MVC)
....................................................................... 64
Section 18.4: 4- Entity Framework @ Unit Test Layer
............................................................................................. 66
Chapter 19: Entity Framework with PostgreSQL
........................................................................................ 70
Section 19.1: Pre-Steps needed in order to use Entity Framework 6.1.3 with PostgresSql using
Npgsqlddexprovider
........................................................................................................................................... 70
Chapter 20: Entity Framework with SQLite
................................................................................................... 71
Section 20.1: Setting up a project to use Entity Framework with an SQLite provider
.......................................... 71
Chapter 21: Advanced mapping scenarios: entity splitting, table splitting
.................................. 74
Section 21.1: Entity splitting
......................................................................................................................................... 74
Section 21.2: Table splitting
........................................................................................................................................ 75
Chapter 22: Mapping relationship with Entity Framework Code First: One-to-one and
variations
........................................................................................................................................................................ 76
Section 22.1: Mapping one-to-zero or one
................................................................................................................ 76
Section 22.2: Mapping one-to-one
............................................................................................................................ 79
Section 22.3: Mapping one or zero-to-one or zero
.................................................................................................. 80
Chapter 23: Mapping relationship with Entity Framework Code First: One-to-many and
Many-to-many
............................................................................................................................................................. 81
Section 23.1: Mapping one-to-many
.......................................................................................................................... 81
Section 23.2: Mapping one-to-many: against the convention
............................................................................... 82
Section 23.3: Mapping zero or one-to-many
............................................................................................................ 83
Section 23.4: Many-to-many
...................................................................................................................................... 84
Section 23.5: Many-to-many: customizing the join table
........................................................................................ 85
Section 23.6: Many-to-many: custom join entity
..................................................................................................... 86
Credits
.............................................................................................................................................................................. 89
You may also like
........................................................................................................................................................ 90
About
Please feel free to share this PDF with anyone for free,
latest version of this book can be downloaded from:
https://goalkicker.com/EntityFrameworkBook
This
Entity Framework Notes for Professionals
book is compiled from
Stack
Overflow Documentation,
the content is written by the beautiful people at Stack
Overflow. Text content is released under Creative Commons BY-SA, see credits at
the end of this book whom contributed to the various chapters. Images may be
copyright of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not
affiliated with official Entity Framework group(s) or company(s) nor Stack
Overflow. All trademarks and registered trademarks are the property of their
respective company owners
The information presented in this book is not guaranteed to be correct nor
accurate, use at your own risk
Please send feedback and corrections to
web@petercv.com
GoalKicker.com – Entity Framework Notes for Professionals
1
Zgłoś jeśli naruszono regulamin