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


Talk Python's Mastodon Michael Kennedy's Mastodon