Entity Framework Core in Action
Book information
Description
brief contents......Page 4 contents......Page 5 preface......Page 18 about this book......Page 20 --- Start......Page 25 1 Intro to Entity FrameworkCore......Page 26 1.1 What you’ll learn from this book......Page 27 1.2 My “lightbulb moment” with Entity Framework......Page 28 1.3 Some words for existing EF6.x developers......Page 29 1.4.1 The downsides of O/RMs......Page 30 1.6 Your first EF Core application......Page 31 1.6.2 Creating your own .NET Core console app with EF Core......Page 32 1.7 The database that MyFirstEfCoreApp will access......Page 35 1.8.1 The classes that map to the database—Book and Author......Page 36 1.8.2 The application’s DbContext......Page 37 1.9.1 Modeling the database......Page 38 1.9.2 Reading data from the database......Page 40 1.9.3 Updating the database......Page 42 1.10 Should you use EF Core in your next project?......Page 45 1.10.3 Rapid development......Page 46 1.10.7 Stable library......Page 47 1.11 When should you not use EF Core?......Page 48 2 Querying the database......Page 50 2.12.1 The book app’s relational database......Page 51 2.12.3 The final database showing all the tables......Page 53 2.12.4 The classes that EF Core maps to the database......Page 55 2.2.1 Defining the application’s DbContext: EfCoreContext......Page 56 2.2.2 Creating an instance of the application’s DbContext......Page 57 2.2.3 Creating a database for your own application......Page 58 2.3.2 A series of LINQ/EF Core commands......Page 60 2.15.1 Eager loading: loading relationships with the primary entity class......Page 61 2.15.2 Explicit loading: loading relationships after the primary entity€class......Page 63 2.4.3 Select loading: loading specific parts of primary entity class and any relationships......Page 64 2.16.1 Creating the display string of a book’s authors......Page 66 2.5.2 Understanding the limitations of client vs. server evaluation......Page 67 2.6.1 Building the book list query by using select loading......Page 68 2.6.2 Introducing the architecture of the book app......Page 72 2.7.1 Sorting books by price, publication date, and customer ratings......Page 73 2.7.2 Filtering books by publication year and customer ratings......Page 74 2.8 Putting it all together: combining query objects......Page 77 3 Changing the database content......Page 80 3.2 Creating new rows in a table......Page 81 3.2.1 Creating a single entity on its own......Page 82 3.2.2 Creating a book with a review......Page 83 3.3 Updating database rows......Page 86 3.3.1 Handling disconnected updates in a web application......Page 88 3.4.1 Principal and dependent relationships......Page 93 3.4.2 Updating one-to-one relationships—adding a PriceOffer to a book......Page 95 3.4.3 Updating one-to-many relationships—adding a review to a book......Page 98 3.4.4 Updating many-to-many relationships—changing a book’s authors......Page 102 3.5 Deleting entities......Page 105 3.5.1 Using a soft delete—using model-level query filters to “hide” entities......Page 106 3.5.3 Deleting a principal entity that has relationships......Page 107 4 EF Core in business logic......Page 111 4.1 Why is business logic so different from other code?......Page 112 4.2.1 The business rules that you need to implement......Page 113 4.3.1 Five guidelines for building business logic that uses EF Core......Page 114 4.4 Implementing the business logic for processing an order......Page 116 4.4.2 Guideline 2: Business logic should have no distractions......Page 117 4.4.3 Guideline 3: Business logic should think it’s working on in-memory data......Page 119 4.4.4 Guideline 4: Isolate the database access code into a separate project......Page 121 4.4.5 Guideline 5: Business logic shouldn’t call EF Core’s SaveChanges......Page 123 4.4.6 Putting it all together—calling the order-processing business logic......Page 124 4.4.7 Any disadvantages of this business logic pattern?......Page 126 4.5 Placing an order on the book app......Page 127 4.6.1 Validating the data that you write to the database......Page 128 4.6.2 Using transactions to daisy-chain a sequence of business logic code......Page 132 5 EF Core in ASP Core web applications......Page 138 5.2 Understanding the architecture of the book app......Page 139 5.3 Understanding dependency injection......Page 140 5.3.2 A basic example of dependency injection in ASP.NET Core......Page 141 5.3.3 The lifetime of a service created by DI......Page 142 5.4.1 Providing information on the database’s location......Page 143 5.4.2 Registering your application’s DbContext with the DI provider......Page 145 5.5.2 Where does the EF Core code live in the book app?......Page 146 5.6 Implementing the book list query page......Page 148 5.7 Implementing your database methods as a DI service......Page 150 5.7.2 Injecting ChangePubDateService into the ASP.NET action method......Page 151 5.7.3 Improving registering your database access classes as services......Page 152 5.8 Deploying an ASP.NET Core application with a database......Page 154 5.8.2 Creating and migrating the database......Page 155 5.9.1 Updating your production database......Page 156 5.9.2 Having your application migrate your database on startup......Page 157 5.10.1 Why async/await is useful in a web application using EF Core......Page 160 5.10.3 Changing over to async/await versions of EF Core commands......Page 161 5.11 Running parallel tasks: how to provide the DbContext......Page 162 5.11.1 Other ways of obtaining a new instance of the application’s DbContext......Page 165 --- Entity Framework in depth......Page 167 6 Configuring nonrelational properties......Page 168 6.1 Three ways of configuring EF Core......Page 169 6.2 A worked example of configuring EF Core......Page 170 6.3 Configuring By Convention......Page 172 6.3.3 Conventions for name, type, and size......Page 173 6.3.5 An EF Core naming convention identifies primary keys......Page 174 6.4.1 System.ComponentModel.DataAnnotations......Page 175 6.5.1 A better way to structure your Fluent API commands......Page 176 6.6 Excluding properties and classes from the database......Page 178 6.6.2 Excluding a class or property via the Fluent API......Page 179 6.8 Setting database column type, size, and nullability......Page 180 6.9.1 Configuring a primary key via Data Annotations......Page 181 6.10 Adding indexes to database columns......Page 182 6.11.1 Configuring table names......Page 183 6.11.3 Configuring the database column names in a table......Page 184 6.12 Using specific database-provider Fluent API commands......Page 185 6.13.2 Use validation Data Annotations wherever possible......Page 186 6.14.1 Configuring shadow properties......Page 187 6.14.2 Accessing shadow properties......Page 188 6.15.1 Creating a simple backing field accessed by a......Page 189 6.15.2 Configuring backing fields......Page 192 7 Configuring relationships......Page 195 7.1 Defining some relationship terms......Page 196 7.3 Configuring relationships......Page 197 7.4.1 What makes a class an entity class?......Page 198 7.4.3 How EF Core finds foreign keys By Convention......Page 199 7.4.5 Foreign keys—what happens if you leave them out?......Page 201 7.4.6 When does By Convention configuration not work?......Page 202 7.5.1 The ForeignKey Data Annotation......Page 203 7.5.2 The InverseProperty Data Annotation......Page 204 7.6.1 Creating a one-to-one relationship......Page 205 7.6.2 Creating a one-to-many relationship......Page 208 7.6.3 Creating a many-to-many relationship......Page 209 7.7.1 OnDelete—changing the delete action of a dependent entity......Page 210 7.7.2 IsRequired—defining the nullability of the foreign key......Page 212 7.7.3 HasPrincipalKey—using an alternate unique key......Page 214 7.8.1 Owned types—adding a normal class into an entity class......Page 216 7.8.2 Table per hierarchy—placing inherited classes into one table......Page 220 7.8.3 Table splitting—mapping multiple entity classes......Page 224 8 Configuring advanced features and handling concurrency conflicts......Page 227 8.1.2 Our solution—IEnumerable property......Page 228 8.2 DbFunction—using user-defined functions with EF Core......Page 230 8.2.1 Configuring a scalar user-defined function......Page 231 8.2.2 Adding your UDF code to the database......Page 233 8.3 Computed column—a dynamically calculated column value......Page 234 8.4 Setting a default value for a database column......Page 236 8.4.1 Adding a constant as a default constraint......Page 237 8.4.3 Creating a value generator to generate a default value dynamically......Page 238 8.5 Sequences—providing numbers in a strict order......Page 240 8.6.1 Marking a column that’s generated on an addition or update......Page 241 8.6.2 Marking a column’s value as set on insert of a new row......Page 242 8.7 Handling simultaneous updates—concurrency conflicts......Page 243 8.7.1 Why do concurrency conflicts matter?......Page 244 8.7.2 EF Core’s concurrency conflict–handling features......Page 245 8.7.3 Handling a DbUpdateConcurrencyException......Page 251 8.7.4 The disconnected concurrent update issue......Page 254 9 Going deeper into the DbContext......Page 259 9.2 Understanding how EF Core tracks changes......Page 260 9.3 Details on every command that changes an entity’s State......Page 261 9.3.1 The Add command--inserting a new row in the database......Page 262 9.3.2 The Remove command—deleting a row from the database......Page 264 9.3.3 Modifying a tracked entity—EF Core’s DetectChanges......Page 265 9.3.4 INotifyPropertyChanged entities—a different way of tracking changes......Page 267 9.3.5 The Update method—telling EF Core that everything has changed......Page 269 9.3.6 The Attach method—changing an untracked entity into a tracked entity......Page 271 9.3.7 Setting the State of an entity directly......Page 272 9.3.8 TrackGraph—handling disconnected updates with relationships......Page 273 9.4 Using ChangeTracker to detect changes......Page 275 9.5 Using raw SQL commands in EF Core......Page 277 9.5.1 FromSql—adding raw SQL to an EF Core query......Page 278 9.5.2 ExecuteSqlCommand—executing a nonquery command......Page 279 9.5.3 Reload—useful after an ExecuteSqlCommand......Page 280 9.5.4 GetDbConnection—calling database access commands......Page 281 9.6.1 Using the Model property to build a fast database wipe method......Page 282 9.7.1 Handling database transactions with EF Core’s execution strategy......Page 285 9.7.2 Altering or writing your own execution strategy......Page 286 --- Entity Core in real-world applications......Page 289 10 Patterns for EF Core applications......Page 291 10.1 Another look at the separation-of-concerns principle......Page 292 10.2 Using patterns to speed development of database access......Page 293 10.3 Speed up query development—use a LINQ mapper......Page 294 10.4 Domain-driven-design database repository......Page 298 10.4.1 Example Book DDD entity and repository......Page 300 10.4.2 How the DDD design changes the business logic design......Page 304 10.4.3 Impressions from building this DDD design......Page 307 10.5.1 Some forms of Repository patterns to avoid......Page 309 10.6.1 Creating DbContexts that contain only a subset of entities/tables......Page 310 10.6.2 Passing data between bounded contexts......Page 312 10.7.1 Data validation to your entity classes makes for better error feedback......Page 313 10.7.2 Business logic should contain checks and return a list of all errors......Page 314 10.7.3 Catching database server errors and providing user-friendly feedback......Page 315 11 Handling database migrations......Page 320 11.1.1 A view of what databases need updating......Page 321 11.2 Code-first: using EF Core’s migrations......Page 322 11.2.1 Stage 1: creating a migration—building the code for migration......Page 323 11.2.2 Stage 2: applying migrations—updating a database schema......Page 328 11.2.3 Undoing a migration—Remove-Migration or update command......Page 331 11.3 Database-first: creating a DbContext from a database......Page 332 11.3.1 How to alter or edit the output from the scaffold command......Page 334 11.3.2 The limitations of the reverse-engineering feature......Page 337 11.4 SQL-first: using SQL change scripts to change the schema......Page 338 11.4.1 Using an SQL comparison tool to build an SQL change script......Page 340 11.4.2 Using EfSchemaCompare to check your SQL matches EF Core’s model......Page 341 11.5 Part 2—Issues around a database schema change......Page 342 11.5.1 Applying nonbreaking changes while the current app is running......Page 343 11.5.2 Applying breaking database changes by stopping the application......Page 344 11.5.3 Handling breaking database changes when you can’t stop the app......Page 348 12 EF Core performance tuning......Page 352 12.1.1 “Don’t performance tune too early” doesn’t mean you stop thinking......Page 353 12.1.2 How do you decide what’s slow and needs performance tuning?......Page 354 12.1.3 The cost of finding and fixing performance issues......Page 355 12.2 Part 2—Techniques for diagnosing a performance issue......Page 356 12.2.1 Stage 1: get a good overview—measuring the user’s experience......Page 357 12.2.3 Stage 3: inspecting the SQL code to find poor performance......Page 358 12.3 Part 3—Techniques for fixing performance issues......Page 361 12.4.1 Using Select loading to load only the columns you need......Page 362 12.4.3 A warning that using lazy loading will affect database performance......Page 363 12.4.6 Ensuring that your database access code is isolated/decoupled......Page 364 12.5.1 Not minimizing the number of calls to the database......Page 365 12.5.2 Calling SaveChanges multiple times......Page 366 12.5.3 Allowing too much of a data query to be moved into the software side......Page 367 12.5.4 Not replacing suboptimal SQL translations with user-defined functions......Page 368 12.5.5 Not precompiling queries that are used frequently......Page 370 12.5.7 Not using the Find method when an entity might be already loaded......Page 371 12.6.1 Making DetectChanges work too hard......Page 372 12.6.2 Startup issue: using one large DbContext......Page 373 12.7.1 Using pooling to reduce the cost of a new application’s DbContext......Page 374 12.7.2 Async/await—adding scalability, with small effect on speed......Page 375 12.7.4 Picking the right architecture for applications that need high scalability......Page 376 13 Example of performance tuning......Page 378 13.1.1 Analyzing the book list query to see potential performance issues......Page 379 13.1.2 Turning the book’s Votes display into a client-side calculation......Page 381 13.2 Part 1b—Improving the query by adding a DbFunction......Page 383 13.2.1 Looking at the updated query......Page 385 13.2.2 Ensuring that the query sorting and filtering are performing well......Page 386 13.3.1 Introducing Dapper......Page 388 13.3.2 Rewriting MapBookToDto and associated EF queries using Dapper......Page 389 13.4.1 Creating an ActualPrice property—changing the promotion process......Page 393 13.4.2 Caching the book review values, and not letting them get out-of-date......Page 396 13.4.3 Calculating AuthorsString when a book is first created......Page 403 13.4.4 Analyzing the changes—Is the performance gain worth the effort?......Page 404 13.5 Comparing parts 1a, 1b, 2, and 3......Page 406 13.6 Database scalability—what can you do to improve that?......Page 408 14 Different database types & EF Core services......Page 410 14.1 What differences do other database server types bring?......Page 411 14.1.1 Creating an instance of the application’s DbContext for MySQL......Page 412 14.1.2 What you have to do to convert the SQL Server application to MySQL......Page 413 14.1.3 Looking at other database server types and differences......Page 414 14.1.4 Summarizing EF Core’s ability to work with multiple database types......Page 415 14.2 Developing a CQRS architecture application with EF Core......Page 416 14.2.1 Implementation of a two-database CQRS architecture application......Page 417 14.2.2 How the parts of the CQRS solution interact with each other......Page 419 14.2.3 Finding book view changes—Part 1, finding the correct state and key......Page 423 14.2.4 Finding the book view changes—Part 2, building the correct State......Page 425 14.2.5 Why the CQRS solution is less likely to have out-of-date cached values......Page 428 14.2.6 Is the two-database CQRS architecture worth the effort?......Page 429 14.3.1 Accessing an EF Core service to help in your own application......Page 433 14.3.2 Replacing an EF Core service with your own modified service......Page 434 14.4 Accessing command-line tools from software......Page 436 14.4.1 How to access EF Core design-time services......Page 437 14.4.2 How to use design-time services to build the EfSchemaCompare tool......Page 438 15 Unit testing EF Core applications......Page 441 15.1 Introduction—our unit test setup......Page 442 15.1.1 The test environment—the xUnit unit test library......Page 443 15.1.2 A library I’ve created to help with unit testing EF Core applications......Page 444 15.2.1 The options you have for simulating the database......Page 445 15.2.2 Choosing between an in-memory or real database for unit testing......Page 446 15.3.1 The application’s DbContext options are provided via its constructor......Page 447 15.3.2 Setting an application’s DbContext options via OnConfiguring......Page 448 15.4 Simulating a database—using an in-memory database......Page 450 15.5.1 How to set up a real database for unit testing......Page 452 15.5.2 Running unit tests in parallel—uniquely named databases......Page 453 15.5.3 Tips on how to speed up the database creation stage of a unit test......Page 455 15.5.4 How to handle databases in which you’ve added extra SQL code......Page 458 15.6 Unit testing a disconnected state update properly......Page 459 15.7 Mocking a database repository pattern......Page 461 15.8 Capturing EF Core logging information in unit testing......Page 463 15.8.1 Using logging to help you build SQL change scripts......Page 466 15.9 Using the EfSchemaCompare tool in your unit tests......Page 467 15.9.1 Features and options for the EfSchemaCompare tool......Page 469 Brief introduction to LINQ......Page 471 Early information on EF Core version 2.1......Page 481 index......Page 491
Similar books
MySQL® Notes for Professionals book
2018 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36
2010 · PDF
THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.
1858 · PDF
Idries Shah 27 Books Collection : A Perfumed Scorpion, A Veiled Gazelle, Caravan of Dreams, Darkest England, Destination Mecca, Evenings with Idries Shah, Knowing How to Know, Learning How to Learn, Letters and Lectures of Idries Shah, Neglected aspects of Sufi study, Observations, Oriental Magic, Reflections, Seeker after Truth, Special Illumination, Special Problems in the study of Sufi ideas, Sufi thought and action, Tales of the Dervishes, The Dermis Probe, The Elephant in the Dark, The Englishman Handbook, Idries Shah Antology, The Magic Monastery, The natives are restless, wisdom of the Idiots PDF.
2022 · PDF
The travels of Capts. Lewis and Clarke from St. Louis, by way of the Missouri and Columbia rivers, to the Pacific ocean; performed in the years 1804, 1805 & 1806, by order of the government of the United States. Containing delineations of the manners, customs, religion, &c. of the Indians, comp. from various authentic sources, and original documents, and a summary of the Statistical view of the Indian nations, from the official communication of Meriwether Lewis. Illustrated with a map of the country, inhabited by the western tribes of Indians
1809 · PDF
Professional Linux kernel architecture ''Wrox programmer to programmer''--Cover. - ''What you are reading right now is the result of an evolution over more than seven years: After two years of writing, the first edition was published in German by Carl Hanser Verlag in 2003. It then described kernel 2.6.0. The test was used as a basis for the low-level design documentation for the EAL4+ security evaluation of Red Hat Enterprise Linux 5, requiring to update it to kernel 2.6.18 (if the EAL acronym does not mean anything to you, then Wikipedia is once more your friend). Hewlett-Packard sponsored the translation into English and has, thankfully, granted the rights to publish the result. Updates to kernel 2.6.24 were then performed specifically for this book''--P. ix
2008 · PDF