Download presentation
Presentation is loading. Please wait.
Published byIrene Samuelsen Modified over 5 years ago
1
06 | Integrating extra features and looking forward
Christopher Harrison | Content Developer, Microsoft Adam Tuliper | Technical Evangelist, Microsoft
2
Module Overview Stored procedures
Increasing user experience with concurrency detection A few best practices Looking forward with Entity Framework 7
3
Stored procedures
4
Store procedures Why use stored procedures? Limits dynamic sql
Single point to secure – easier permissions Know exactly how DB is being queries Limits dynamic sql Still possible with sprocs, just more manual effort In general performance is _not_ a reason Virtually the same as sql query (pendulum swings both ways)
5
Stored procedures Support came as XX
In the past could simply Database.SqlQuery Could map in the designer (which is going away) Code first will generate procedures context.Database.SqlQuery<myEntityType>( new SqlParameter("param1", param1), new SqlParameter("param2", param2), new SqlParameter("param3", param3) );
6
Integrating stored procedures
7
Increasing user experience with concurrency detection
8
Concurrency As Chris showed, easy to implement via concurrency token
[TimeStamp] modelBuilder.Entity<YourEntity>() .Property(e => e.Timestamp) .IsRowVersion(); Change detection can be enhanced via MVC Action Filters Let’s give the user the choice on how to proceed
9
Concurrency detection and user choice
10
A few best practices
11
Helpful hints in a web world
Since the web layers (controller to view) are disconnected Always convert results to IEnumerable via .ToList() Ex. context.Albums.Where(o=>o.Title.Contains(search).ToList() Disable lazy loading in web environments context.Configuration.LazyLoadingEnabled = false; Use .Include to load related entities DbContext is not thread safe. Do not cache it! Always dispose DbContext when done
12
Context lifetime When context is gone, connection is gone
Can’t (and shouldn’t) use connection from MVC view Get data, send to view Always execute result set when needed before exiting using() using (var context = new MusicContext()) { var albums = context.Albums .Where(o=>o.Title.Contains(search)) .ToList()); return View(albums); }
13
Watch out for… If you have two+ context classes
One from selecting auth during app creation (Identity) One for your other entities Migrations should specify separate folders Enable-Migrations –MigrationsDirectory IdentityMigrations Enable-Migrations –MigrationsDirectory MusicStoreMigrations For complex joins, keep an eye on queries Doing lots of inserts? AutoDetectChangesEnabled = false;
14
Async when appropriate
EF supports async queries Async allows current thread to be used elsewhere Useful in case of network, db delays (long operations) Although not all the time, caution of overloading db public async Task<ActionResult> Create(…) { if (ModelState.IsValid) db.Albums.Add(album); await db.SaveChangesAsync(); return RedirectToAction("Index"); } return View(album);
15
Consider Connection resiliency
This is a connection retry policy Works great with async Four modes DefaultExecutionStrategy DefaultSqlExecutionStrategy DbExecutionStrategy SqlAzureExecutionStrategy throws RetryLimitExceededException
16
Connection Resiliency & Async
17
Looking forward with Entity Framework 7
18
Notable new features Designer has been removed
Code first only Rewritten from ground up with performance in mind New platforms New data stores In memory database Update-Database now split Apply-Migration Script-Migration
19
EF 7 Quick Tour
20
Resources Additional patterns Reducing Code First Database Chatter
ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work- patterns-in-an-asp-net-mvc-application Reducing Code First Database Chatter EF7 Project home
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.