.NET Oxford Meetup XIV: Entity Framework Core


Last week, we had our May meetup with guest speaker Jon P Smith, telling us all about Entity Framework Core! And Jon did not only one, but two EF packed talks! The first explaining Entity Framework Core, and after the break, the second talking about performance tuning Entity Framework!

I was running a bit late for this one (when I say late, I mean late for an organiser, not late for the event start!). When I arrived, I was surprised to find that the front gates to the venue were locked! A few attendees then started to appear looking equally as confused as I was. I tried ringing the venue a few times, but couldn't get through. Eventually I managed to get through to them, and it looks like someone had mistakenly locked the gates. Matt and a few others were already in there, so it must have been locked after they arrived. So if anyone turned up and saw a locked gate, and went away - then I'm really sorry about that - we've spoken to the venue about it to ensure it won't happen again! I'm just glad I was running late, otherwise we may not have seen it had been locked at all, and a lot more members might have left assuming it had been cancelled!

jon

Intro Talk

As usual, before the main talks, we had the intro talk - welcoming everyone, thanking the sponsors, detailing some news items, and also doing the prize draw! This time, it was Matt's turn to do the intro, meaning that I had pretty much nothing to do at the event, which always feels a bit strange!

The slides from the intro talk can be found here, and the Reveal.js source code can be found here.

matt

News Items

The timing of this meetup coincidentally fell on day two of the three-day Microsoft Build Conference, which always has a huge amount of news items. However, this didn't give Matt much time to prepare! I believe he was up quite late going through this the night before the meetup!

Below are some of the news items he picked out. He also pointed everyone at a link to the main MS Build page where everyone can catch up on all of the announcements that were (and hadn't yet been at the time of the meetup) announced.

Visual Studio Live Share:

We've spoken about Visual Studio Live Share before when it was first announced, but unfortunately, it wasn't publicly available to play with. However, at the build conference, they announced that this is now in public preview! I wanted to do a live demo in the intro talk, but unfortunately we didn't have enough time before the meetup to test this out and set it up. Maybe at the next one! ;)

ML.NET:

Another announcement was ML.NET, which is an open-source, cross platform, machine learning library from Microsoft. Quoting their Github page ...

ML.NET allows .NET developers to develop their own models and infuse custom ML into their applications without prior expertise in developing or tuning machine learning models, all in .NET.

IntelliCode:

Microsoft also announced IntelliCode, which brings Artificial Intelligence to your intellisense experience in Visual Studio. I guess personally, I'm less excited about this, as I feel Resharper does a pretty good job of guessing what I want to type anyway, and I'm not that sure what extra this could bring to the table. It's certainly interesting hearing about all the different things AI is being used for though!

Corriculo Recruitment

Our amazing sponsors Corriculo Recruitment where on hand, and as usual, did an incredible job. They've been our sponsors from the very beginning, and I really cannot thank them enough - not only helped us out financially - covering the venue costs, and providing plenty of liquid refreshments at each of our events - but also, helping us out welcoming everyone in, with marketing and promotion, feedback and advice, and much more!

Prize Draws

Our prizes are from our amazing prize draw sponsors - JetBrains, Manning Publications, and Oz-Code.

As mentioned last month, we've introduced a 2-week time limit, and will re-draw if we don't hear back from the winners - which unfortunately is becoming a very common occurrence. If you win, but don't want the prize, please just reply and say so, rather than not replying at all.

Jetbrains

Congratulations to Morgan Willis for winning a year-long Jetbrains product licence! We've still not heard back from him, so I can't say which product he choose, but I'll update it here when he does. Unless we hit the 2 week limit we mentioned earlier, then we'll do a re-draw.

Manning Books

Congratulations to Simon Parr for winning a Manning ebook of his choice! We've not heard back from Simon either, so I also can't say which book he has chosen. Again, we'll do a redraw after the 2 week limit if he doesn't get back to us. The winner has the choice of any of the awesome Manning ebooks from their website too!

Remember that we have our special Manning coupon code (ug367) which gives all of our members a 36% discount on any of their e-books! They've also asked me to share a link to some of their new courses for their LiveVideo system.

Oz-Code

Congratulations to Colin Turner for winning the Oz-Code licence! Colin has replied, and is now enjoy the incredible debugging abilities of Oz-Code!

If you haven't checked it out, definitely download the trial and have a play. All our member get a free 3 month trial licence (see below) or 50% off a full licence! To claim, you can visit this link to pick up your licence!

prizesponsors

Jon P Smith: Entity Framework Core

jon

Jon did two talks - before the break, he spoke about what Entity Framework Core was, and also how you use it. Then after the break, he spoke about performance tuning. Afterwards he said to me that he was concerned that he had gone a bit too deep - but I didn't find this at all, and felt it was at just the right level. Our audience is quite a tech savvy bunch, and I also think most will have had experience with Entity Framework before, even if not the Core version.

Jon started off talking about his book, Entity Framework Core in Action, and how he had been approached to write this book because of his blog. A lot of the content of his talk covered parts in the book, and remember that if you want to buy his book, all our members can get 36% off any Manning book with the coupon code ug367. However, Jon has bettered this, and you can get 40% off this particular book and also ASP.NET Core in Action with coupon code 'dotnetmeetup'.

First Talk: An Introduction to how Entity Framework Core Works Inside

Jon did a great job of starting off with a simple example, targeting people who hadn't used Entity Framework before - explaining concepts like Foreign Keys, Navigations Properties, the DbContext, and how you use LINQ to interact with your database - then later moving onto more complicated examples. He also mentioned that EF Core is a complete rewrite, allowing Microsoft to really improve the codebase and remove technical depth without having to worry too much about breaking changes.

He then went a bit deeper, explaining what happens when you make a LINQ query, and how it gets translated to SQL by the provider before being sent to the database. Then on the return trip the query results gets converted into .NET objects and the relationships are mapped. He wrapped up this section up with a demo of a simple book-listing website showing how what he'd explained so far fits together.

Next, he spoke about the different ways the data is loaded - eg. Eager loading, Explicit loading, Select loading, and Lazy loading. Rather than me repeating explanations here, this Microsoft Docs article explains how these work really well. Lazy loading isn't available in EF Core until version 2.1, but as Jon commented on, and I completely agree - it's perhaps a good idea to avoid this anyway, and know where your data is being loaded explicitly.

Jon then explained how the different CRUD operations work (ie. Create, Read, Update, Delete), and on the Delete bit, I learnt something new when he spoke about Query Filters. He explained that this was a good way of doing soft deletes - ie. rather than actually deleting a record from the database, instead have a flag IsDeleted, then have a global query filter to ensure these are never shown. This also seems a good way of doing multi-tenancy. Although, I'd be concerned about these predicates being secretly added to all queries - especially in larger teams where a particular developer might now know this is happening. Although, I guess a slower query is better than a query that mistakenly returns data for another tenant!

Jon then ends his first talk discussing configuring EF, and how data migrations work.

The PDF slides for his first talk can be downloaded here

Second Talk: A Working Example of Performance Tuning Entity Framework Core

After the break, Jon's second talk was about performance tuning. I must admit, I was sortof expecting this to look into using tools like SQL Profiler, and discuss Execution Plans. But actually, I guess those topics are less about Entity Framework, and more about the database side.

Instead, Jon discussed design patterns and code architecture. The first thing he spoke about was to avoid the Repository Pattern. I must admit, that this is one of the few things that I very strongly disagreed with about Jon's talk - but here's not the place to voice my opinions :) Perhaps something for a separate blog post!

The next section was about measuring performance. He explained that he uses two tools to measure performance - Google Chrome's Dev Tools to get total request time, and EF Core's logging to log the SQL itself and the time taken for that query. When writing LINQ for Entity Framework, you're not dealing with the SQL yourself - EF is doing this for you. So having visibility of the generated SQL is important when performance tuning. Jon mentioned how the generated SQL in the Core version of EF, has been vastly improved - which is really good to hear, as I've seen some pretty hairy SQL generated by EF6! Even so though, when performance tuning, it's important to be able to view the generated SQL.

He covered three top performance issues ...

Doing too much on the software side:

This is very important to know - if a query isn't doing all of the filtering on the database-side that you're expecting - you might be pulling way more records than you think you are to your application and doing the filtering in-memory instead of in your database.

An example query from his slides shows this ...

var books = context.Books.ToList().Take(10);

The ToList() forced generation and execution of the SQL, meaning that all of the books are pulled into memory, then the Take(10) takes the top 10 records from the in-memory list!

Jon discussed how EFCore has a new feature called Client vs. Server Evaluation. Previously EF would have thrown an exception if the server didn't support something you were trying to do. It sounds like with this new feature enabled, rather than throwing an exception - it'll automatically do some of the work client-side. This sounds terrifying to me! I WANT it to throw an exception so I know that what I'm doing isn't supported. It turns out this feature can be disabled - which I'd highly recommend doing!

Not minimising the database accesses:

Here he talks about making sure you're not reading too many columns or rows, and also not making too many separate calls to the database. By 'separate', this can mean intentional different calls, but also doing things like explicit loading and lazy loading. I personally prefer forcing as much as I can into one query and avoiding lazy loading - and it sounds like Jon agrees!

Not dealing with sub-optimal SQL:

Going back to Jon's discussion about logging the generated SQL - this can be really useful for seeing what SQL is actually generated. Whilst EF might be pretty good most of the time, sometimes you can do it better yourself if you've found a better and faster way of writing a specific query. He discusses using a library called Dapper to write your own SQL, which I'm also a huge fan of. I've always used either EF or Dapper for projects. However, Jon showed how you can do this ...

using(var context = new EfCoreContext())
{
    var books = context.Database
        .GetDbConnection()
        .Query<Book>("SELECT * FROM Books");
}

I hadn't considered before getting the DbConnection straight from Entity Framework to mix and match both technologies! I really like this idea - great tip Jon!

Jon ended his talk discussing using the CQRS pattern to separate reads and writes, then using a NoSQL database to cache data that's already transformed into the shape you want to read.

I really enjoyed both of Jon's talks, and thought they were all really nice and clear. It would be remiss of me not to end by reminding everyone about his book, Entity Framework Core in Action, and that you can get an entire 40% off it using he coupon code dotnetmeetup!

A massive thanks again to Jon for making the trip for both talks!

The PDF slides for his second talk can be downloaded here

Dev Tips

We've recently introduced 30 second dev tips at the end where anyone can get up and do a dev tip.

I started first with my tip about using the command line ...

"In a few talks I've done recently, which have bits related to the command line, I've asked the audience who uses the command line, and have been surprised by how few hands go up. My dev tip was to use and embrace the command line. There are some fantastic command line tools that can help you with your job. I'm not saying that the CLI is better than GUIs, or vice-versa - but if you skip out CLI tools, and only use GUIs, then you're missing our on a large selection of tools. So use both CLI and GUI tools - but don't neglect either. I've recently written a blog post about Getting more from the Windows Command Line which is worth a read."

Next up was James World with his tip ...

"Data tip: Never use a table column for more than one purpose. If you are changing the purpose of a column, or transforming how data is represented, add a new column instead and remove the old column - later if it makes sense (such as until you are sure the new feature is working)."

Our speaker, Jon P Smith also did one ...

"After a project then ask yourself three questions:

  • What went well (so you can do it again)
  • What went badly (any how would you fix that in the next project)
  • What was repetitive (is it worth building a library?)

The other comment is – you don't do this to beat yourself up, but to learn from your experiences. You tend to learn more from mistakes, so the ‘bad’ bits are just you learning more quickly."

As noone else wanted to do one - I snook in an extra one before we wrapped up and headed off to the pub ...

"Jon mentioned in his talk about logging the SQL that Entity Framework is generating. There's a free tool from Stackify called Prefix. This shows you lots of extra information about your local .NET and Java websites' - eg. logs, exceptions, and also SQL statements that are made. This is especially useful when using Entity Framework when the SQL that EF generates isn't always what you'd expect!"

When doing this final tip, James pointed out that there's also Glimpse, which does a similar thing. I haven't tried this in years, so will have to have another play with it! At the time of writing though, their site seems to be down - hopefully it's just temporary!

Upcoming Meetups

All of 2018 has now been mapped out, and we have some very cool talks planned! The June and July events have already been announced on our Meetup.com Event page, but below is the full plan ...

June 5th: Lightning talks:

Our next meetup will be another lighting talk event! This'll be our 3rd lightning talk meetup, and they're always great fun. We have a fantastic lineup with the following talks ...

  • James World: "WebAssembly and Blazor"
  • Stuart Leeks: "Azure Durable Funkiness"
  • Dan Clarke: "Kubernetes - What and Why?"
  • Matt Nield: "Beginner's guide to Azure Search"
  • Corriculo Recruitment: "(title pending)"
  • Dan Clarke: "Why Vi?" (a filler if no-one else wants the last slot)

Note that I'm down for an additional talk at the end as a filler because we had 10 minutes free. As I'm already doing the Kubernetes talk - if you do want a 10-minute slot, then let us know, and you can take the place of my Why Vi? talk.

July 3rd: Getting Entangled in Q#!:

I'm really excited about this one! We have Frances Tibble and Anita Ramanan joining us to talk all about Quantum programming and Microsoft's new Q# programming language! There will even be a hands-on coding session for those who'd like to bring along their own laptops and follow along!

August 7th: CosmosDB:

This hasn't been announced on Meetup.com yet, but should be there in the next few days, but we have James Broome and Mike Larah joining us from endjin talking about CosmosDB!

September 11th: "Pilot Decision Management" and "Getting Chatty with Fish":

In September we have two talks - Clifford Agius will be talking about Pilot Decision Management, and how their training relates and can benefit programmers. Then we also have .NET Oxford co-founder, Matt Nield talking about using Google Assistant to access your data.

October (9th?): Jon Skeet:

October should be with Jon Skeet himself! He has a few talks, and we're not yet sure which one he's doing - but, it's Jon Skeet! He can talk about anything he likes! At the moment this is pencilled in for the 9th, but that hasn't been confirmed yet.

November: Performance in the Javascript Era:

November will be with Benjamin Howarth, talking about various different aspects of performance in an era where much more functionality has been pushed to the frontend. This isn't just a Javascript talk though - it covers a lot of aspects that are important to us .NET developers too!

December: More Lightning Talks!

We thought that December would be a nice place to slot in another lightning talk event. It may be a long way off, but if you do want to do one, feel free to get in touch! First timers are most welcome too!


Please retweet if you enjoyed this post ...

Search


Recent Posts


Featured Posts


.NET Oxford Links