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
4. Store procedures
• Why use stored procedures?
– 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 in EF 6
– Only Fluent API based
• In the past could use procs manually
– var album = Database.SqlQuery<TriviaOption>("dbo.Proc….");
• Could map in the designer (which is going away)
• Code first will generate procedures for
– Insert, Delete, Update
• Wait – why no Select?
8. Concurrency
• As Christopher 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
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
– context.Albums.Include(o=>o.AlbumDetails)
• 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…
• For complex joins, keep an eye on queries
– Remember context.Database.Log = s => Debug.WriteLine(s);
• Doing lots of inserts? AutoDetectChangesEnabled = false;
– EF detects changes on add to context and on save
– Can turn off and on again in same context lifetime
{
context.Configuration.AutoDetectChangesEnabled = false;
Do something
context.Configuration.AutoDetectChangesEnabled = true;
}
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
18. Notable new features
• Designer has been removed – Code First Only
• Rewritten from ground up with performance in mind
• New platforms
– Linux, OSX, Mono, Windows Phone*, Windows Store*
• New data stores
• In memory database
• Update-Database now split
– Apply-Migration
– Script-Migration