Python for the .NET developer Transcripts
Chapter: Database access and ORMs in Python
Lecture: Our C# and Entity Framework web app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Hey, look where we are. We're over on Windows 10.
0:03 So we're not on my Mac right now, and it's back to
0:06 I moved us over to Windows 10 for this particular chapter
0:09 for the C# side of things.
0:11 And the reason that I did
0:12 is we're going to be using SQL Server.
0:14 So over here I have Visual Studio 2019
0:16 and we have SQL Server just running SQL Express
0:20 but, you know, could be anything running there.
0:23 I have that and if we open it up
0:25 you'll see that we're going to have a database.
0:28 Here's our guitars, and if we go to the tables
0:31 you can see, just going to select some stuff out of here.
0:36 Here's our guitars and guess what?
0:38 This should look super familiar.
0:39 Our Axe Black and our Wheezer Classic
0:42 and our Acoustic Black, the urls
0:44 this is just the same data put into this table here.
0:49 Notice that we have some various keys
0:51 we have some indexes, and then all that kind of good stuff.
0:54 And I guess we could also, real quick
0:56 look at the table like this, there's not a whole lot to see
0:59 'cause there's no relationships, but this is what we got.
1:02 Okay, so we have this running over here
1:05 and the way we created it was
1:06 we went and defined a guitar class just like we had before
1:10 but instead we're going to use this
1:12 with Entity Framework code first.
1:15 Though this part is the Entity Framework code first
1:18 and then here's the leftovers that lets us
1:20 sort of create more easily.
1:22 We have to have a empty constructor
1:24 here's the things we're storing in the database
1:26 and there's a convention that that's going to
1:28 be the primary key
1:30 over here and see the primary key right there.
1:32 That's going to be the primary key if we just have an id.
1:35 Okay, so nothing too fancy.
1:37 But then we're going to go define a DB context
1:41 and this is the unit of work and a little bit more
1:44 and on here we're going to say there's a
1:45 DB set of guitar called Guitars.
1:48 And that's why over in our database we have a Guitars.
1:52 Also just setting up to go use SQL Server.
1:54 Okay, so it looks like everything is all set up
1:58 and now to see how we might use it
2:00 let's go to our home controller
2:02 and it just creates this Guitars view model
2:05 which ultimately goes to the CatalogService.
2:07 Remember this, AllGuitars, with the style here?
2:10 So what we do in C# is we
2:12 okay, we're going to create a new DB context
2:15 the specific one that has our tables.
2:17 And this is the, basically unit of work, rolls back
2:21 or we have to go and commit it here.
2:23 And we view our code.
2:24 If it's, there's no filtering
2:26 we're just going to do a LINQ over to Entity Framework
2:30 say I want to go to the guitars table
2:32 order them by descending price
2:34 and then convert that to array, boom that goes back.
2:36 But if we're filtering, then we're going to do
2:38 a cooler LINQ expression here.
2:41 I just love LINQ, this is a beautiful part of the language.
2:43 So from the g in the guitar, the guitar in the guitars
2:47 we're going to just say where the style is equal to the style
2:49 order by price ascending and select g to array.
2:53 Well, that's probably all we really need to see
2:55 for the moment, let's go ahead and run it, see what we get.
2:57 This is another ASP.NET Core web application
3:00 just the one we had from before
3:02 but swapping out the data access from in memory
3:04 to be the database.
3:12 Well, here it is, if we click on this, what do we get?
3:14 Hey, hey, look, we get guitars.
3:16 We have electric guitars, we have acoustic guitars
3:18 that's running our LINQ expression right there
3:21 as I click between those.
3:22 If I click All Guitars, it runs the first one.
3:24 So you can see, our little database access
3:27 it's a thing of beauty, it's working great.
3:29 The only other consideration is
3:30 how did that data get into the database
3:32 in the first place, right?
3:34 Here it is, how did it get there?
3:36 If we look, I also created this data loader thing
3:39 and it does something very similar
3:40 it goes and ensures the database is created.
3:43 Make sure you do that, otherwise it might crash.
3:46 And then we're just going to go down here
3:48 and really quickly see if there's any data already loaded
3:51 if there are, we're done, otherwise we're going to get this
3:55 basically the same data we had in memory
3:56 and we're just going to add it to the database
3:59 and then call db.SaveChanges to close out
4:02 our unit of work.
4:03 Alright, so we run this once during app startup
4:06 and then once it's been run, it's already populated
4:09 then our app just runs like talking to the database
4:12 with existing data. So this is the C# version
4:15 how we went from having just the in memory version
4:18 over to working with the database in an Entity Framework.