Page 13 - MSDN Magazine, July 2018
P. 13
Or if I began with context.Magazines.FromSql with the same query I just showed you, the runtime error would complain about MagazineId not being available.
A better use of this feature is to predefine a type and make sure the DbContext is aware of that type, either by defining a DbQuery or specifying modelBuilder.Query for the type in OnModel- Creating. Then you can use FromSql to query and capture the results. As a somewhat contrived example, or perhaps I should say even more contrived than some of the examples I’ve used already, here’s a new class, Publisher, that’s not an entity or part of my PublicationsContext:
public class Publisher {
public string Name { get; private set; }
public int YearIncorporated { get; private set; } }
It, too, is a read-only class, as I have another application where I maintain Publisher data.
I created a DbQuery<Publisher> named Publishers in my con- text, and now I can use that to execute raw SQL query:
var publishers=_context.Publishers
.FromSql("select name, yearfounded from publishers")
.ToList();
Raw SQL can also be a call to execute a stored procedure. As long as the schema of the results match the type (in this case, Publisher), you can do that, even passing in parameters.
If you’ve been holding off on using EF Core until it was production-ready, the time has finally come.
Putting the Polish on EF Core
If you’ve been holding off on using EF Core until it was produc- tion-ready, the time has finally come. EF Core 2.0 made a great leap in features and functionality, and version 2.1 now includes features that put a real polish on the product. The wait for features from EF6 to appear in EF Core has been due in part to the fact that the EF team has not just copied the old implementations but found smarter, more functional implementations. Query types are a great example of this, compared to the way that views and raw SQL were supported in earlier versions of Entity Framework. Be sure to check out the other new features in EF Core 2.1 by reading the “New Features in EF Core 2.1” section of the EF Core documentation at bit.ly/2IhyHQR. n
Julie lerman is a Microsoft Regional Director, Microsoft MVP, software team coach and consultant who lives in the hills of Vermont. You can find her presenting on data access and other topics at user groups and conferences around the world. She blogs at the thedatafarm.com/blog and is the author of “Programming Entity Framework,” as well as a Code First and a DbContext edition, all from O’Reilly Media. Follow her on Twitter: @julielerman and see her Pluralsight courses at juliel.me/PS-Videos.
Thanks to the following Microsoft technical expert for reviewing this article: Andrew Peters
msdnmagazine.com
Automate and add interactivity to your PDF applications
.NET IMAGING and PDF
Interactive form field JavaScript actions
Barcode and Signature field Annotation and Content editing
Professional SDK for building document management apps
Image Viewer for .NET, WPF and WEB
100+ Image Processing and Document Cleanup commands
PDF Reader, Writer, Visual Editor Image Annotations
JBIG2 and JPEG2000 codecs OCR and Document Recognition Forms Processing and OMR DICOM decoder
Barcode Reader and Generator TWAIN scanning
Free evaluation version
Royalty free licensing
www.vintasoft.com VintaSoft is a registered trademark
of VintaSoft Ltd.