Download presentation
Presentation is loading. Please wait.
Published byVirgil Chase Modified over 5 years ago
1
Removing the SELECT * Chris Woodruff Quicken Loans
2
Platform Strategist Technology /chris.woodruff @cwoodruff
4
friends
5
teamwork
6
partnerships
7
trust but verify
9
Developers use data in many ways…
10
web
11
mobile web
12
api mobile web
13
api mobile web data science
14
home
15
work home
16
car work home
17
car work home traveling
20
let’s look at LINQ private static void Main(string[] args) {
var context = new SchoolContext(); var studentsWithSameName = context.Students .Where(s => s.FirstName == GetName()) .ToList(); } public static string GetName() return “Chris";
21
SQL let’s look at LINQ exec sp_executesql N'SELECT [s].[StudentId],
[s].[DoB], [s].[FirstName], [s].[GradeId], [s].[LastName], [s].[MiddleName] FROM [Students] AS [s] WHERE [s].[FirstName] = nvarchar(4000)’, @__GetName_0=N’Chris’ Go SQL
22
include example var context = new SchoolContext();
var studentWithGrade = context.Students.Where(s => s.FirstName == "Bill") .Include(s => s.Grade) .Include(s => s.StudentCourses) .FirstOrDefault(); SELECT TOP(1) [s].[StudentId], [s].[DoB], [s].[FirstName], [s].[GradeId],[s].[LastName], [s].[MiddleName], [s.Grade].[GradeId], [s.Grade].[GradeName], [s.Grade].[Section] FROM [Students] AS [s] LEFT JOIN [Grades] AS [s.Grade] ON [s].[GradeId] = [s.Grade].[GradeId] WHERE [s].[FirstName] = N’Bill’
23
multiple queries to DB var context = new SchoolContext();
var studentWithGrade = context.Students.Where(s => s.FirstName == "Bill") .Include(s => s.Grade) .Include(s => s.StudentCourses) .FirstOrDefault();
24
SQL SELECT TOP(1) [s].[StudentId], [s].[DoB], [s].[FirstName],
[s].[GradeId], [s].[LastName], [s].[MiddleName], [s.Grade].[GradeId], [s.Grade].[GradeName], [s.Grade].[Section] FROM [Students] AS [s] LEFT JOIN [Grades] AS [s.Grade] ON [s].[GradeId] = [s.Grade].[GradeId] WHERE [s].[FirstName] = N'Bill’ ORDER BY [s].[StudentId] Go SELECT [s.StudentCourses].[StudentId], [s.StudentCourses].[CourseId] FROM [StudentCourses] AS [s.StudentCourses] INNER JOIN ( SELECT DISTINCT [t].* FROM ( SELECT TOP(1) [s0].[StudentId] FROM [Students] AS [s0] LEFT JOIN [Grades] AS [s.Grade0] ON [s0].[GradeId] = [s.Grade0].[GradeId] WHERE [s0].[FirstName] = N'Bill’ ORDER BY [s0].[StudentId] ) AS [t] ) AS [t0] ON [s.StudentCourses].[StudentId] = [t0].[StudentId] ORDER BY [t0].[StudentId]
25
ORM not OOM ORM (Object Relational Mapper) not Object-oriented modeling (OOM)
26
entity data model (EDM)
DB Model First entity data model (EDM)
27
entity data model (EDM)
DB Model First DB Code First entity data model (EDM)
28
entity data model (EDM)
DB Model First DB Code First Code First entity data model (EDM)
29
entity data model (EDM)
DB Model First DB Code First Code First entity data model (EDM)
30
transactions with Entity Framework Core
31
using (var transaction = new System.Transactions.
TransactionScope()) { var database = new DatabaseContext(); var userA = database.Users.Find(1); var userB = database.Users.Find(2); userA.Name = "Admin"; database.SaveChanges(); userB.Age = 28; transaction.Complete(); }
32
var database = new DatabaseContext();
var userA = database.Users.Find(1); var userB = database.Users.Find(2); userA.Name = "Admin"; userB.Age = 28; database.SaveChanges();
33
EF Core features that help the DBA
34
string interpolation in raw SQL
35
string interpolation var city = "Redmond";
using (var context = CreateContext()) { SELECT * FROM Customers WHERE City = {city}"); }
36
SQL string interpolation exec sp_executesql N'SELECT * FROM Customers
WHERE City = nvarchar(4000)’, @p0=N’Redmond’ SQL
37
string interpolation var citstring description = "Smith";
SqlParameter parameter = new SqlParameter("LastName", SqlDbType.NVarChar); parameter.Direction = ParameterDirection.InputOutput; parameter.Size = description.Length; parameter.Value = description;y = "Redmond";
38
SQL string interpolation exec sp_executesql N'SELECT * FROM Customers
WHERE City = nvarchar(5)’, @p0=N’Redmond’ SQL
39
stored procedures var name = "Bill";
var context = new SchoolContext(); var students = context.Students . FromSqlInterpolated($"GetStudents {name}") .ToList();
40
query types
41
keyless entity types
42
mapping to views without primary keys
43
mapping to tables without primary keys
mapping to views without primary keys
44
mapping to queries defined in the model
mapping to tables without primary keys mapping to queries defined in the model mapping to views without primary keys
45
serving as the return type for FromSql() queries
mapping to tables without primary keys mapping to queries defined in the model mapping to views without primary keys serving as the return type for FromSql() queries
46
keyless entity types public DbQuery<BlogPostsCount> BlogPostCounts { get; set; } modelBuilder.Query<BlogPostsCount>().ToView("View_BlogPostCounts") .Property(v => v.BlogName).HasColumnName("Name"); public class BlogPostsCount { public string BlogName { get; set; } public int PostCount { get; set; } }
47
query tags
48
query tags var albums = _context.Album.Include(a => a.Artist) .TagWith("Description: Query for Albums") .TagWith("Query located: Chinook.Controllers.AlbumsController.Index() method") None");
49
SQL query tags -- Description: Query for Albums
-- Query located: Chinook.Controllers.AlbumsController.Index() method -- Parameters: None SELECT [a].[AlbumId], [a].[ArtistId], [a].[Title], [a.Artist].[ArtistId], [a.Artist].[Name] FROM [Album] AS [a] INNER JOIN [Artist] AS [a.Artist] ON [a].[ArtistId] = [a.Artist].[ArtistId]
50
developer EF Core features that help the
51
DbContext Pooling DI container BlogContext BlogContext BlogContext
Request: /blogs/index scope BlogContext blogs controller blogs controller scope BlogContext Request: /blogs/index public class BlogsController : Controller { private BlogContext _context; public BlogsController(BlogContext context) _context = context; } public IActionResult Index() return _context.Blogs.ToList(); blogs controller scope BlogContext Request: /blogs/index
52
DbContext Pooling DI container BlogContext BlogContext BlogContext
Request: /blogs/index context pool scope BlogContext blogs controller BlogContext BlogContext BlogContext Request: /blogs/index BlogContext scope public class BlogsController : Controller { private BlogContext _context; public BlogsController(BlogContext context) _context = context; } public IActionResult Index() return _context.Blogs.ToList(); BlogContext blogs controller BlogContext BlogContext Request: /blogs/index scope BlogContext blogs controller
53
DbContext Pooling DI container BlogContext BlogContext BlogContext
public class BlogsController : Controller { private BlogContext _context; public BlogsController(BlogContext context) _context = context; } public IActionResult Index() return _context.Blogs.ToList(); BlogContext BlogContext
54
DB context pooling services.AddDbContext<BloggingContext>( options => options.UseSqlServer(connectionString)); services.AddDbContextPool<BloggingContext>( options => options.UseSqlServer(connectionString));
55
demo
56
what you should ask your App Devs?
57
Questions You Should Ask
What is the internal level of support / level of service / expected uptime / RTO for this application? Are there any dependencies on enterprise edition features of SQL Server? Are there any other SQL-centric products we need to include in the final architecture? Is this database storing (or processing) privileged data? Will there be interfaces to or from other systems in the environment? What environments need to be created for this solution? What are the expectations around disaster recovery?
58
what do you want to know as a developer?
59
trust but verify
60
teamwork
61
partnerships
63
Chris Woodruff Quicken Loans Thank you! /chris.woodruff @cwoodruff
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.