WPF Entity Framework Core 5 - SQLite

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody in this video we're going to continue working in our application um i'm making the database connection so let's let's get started so the last thing that we did we just finished the product page the way that we wanted to see it without data of course but it is there and we create a categories model and probably we can use let's create another class let's create a product class product and this one should be when i'm being lacing right here fold this over here we will have the id let's call this one product id product name and let's remove the price and let's use the category id right because we're going to link with the category and that will allow us to have a public category category will get set okay let's make this class public just to have it access everywhere and let's go back to the categories one let's make this one public too and i will add over here [Music] a public list of products and it will call products in plural the simple get set equal new right for the automatic implementation pretty much that should be more than enough that we need and this promotes the same stuff that we saw before but now that we have our categories and we have everything we need to do all database context in order to connect to the database so before that we all know we we are not going to be handling the migration the same way that we did before if you want to proceed you know how to do it i just want to keep this as simple as possible so i will just do a right-click in my solution manage my new package i will go to browse and i will look for entity framework core escolite and we're going to install this one the latest table install it let's say that you're going to install a bunch of dependencies with it okay we agree with the agreement for all those dependencies and that one will be and look do we have an issue with category let me see category page yeah we have to go back right here hmm always complain about the changes to go back in plural sorry in uppercase it does for naming convention let me see what is happening with the products too and this one to use this card let's copy this one over here now this one we should be able not click go back perfect and this one should be able to go back there we go yeah so why is complaining line 13. okay we have only one column this is where i had a bunch of spaces there i don't know what is that error yeah let's continue sorry for that let me see if everything is still working the way they're supposed to okay whatever categories page products product page and here we're supposed to be able to select the category and yeah that's it perfect okay sorry for that so we have everything a reader we just inserted or or needed package so we should be able to have a simple um database content and it's called the sqlite database context so it will actually there should be a really good place to put it let's put it inside the model even though it's not a model you will use those so let's put it there and let's put a new class and that one will be called sqlite database context and the only things that i would do with this one is it would be a db context tv context and for that it's complaining we need to import microsoft entity franco core perfect and the only things that we need to do for those is we create a couple of database sets so we call the products and the categories and let's put categories first categorism products right and then we are going to override our configuration so let's do a simple protected override or unconfiguring figurine and we need to have our db context options builder and let's call this options and we need to do options use circulite and here we need to add our connection string and we will need to keep things simple and let's put um let's call this data source equal let's call this db in the sqlite right and you need to call or base the on configuring and we need to send these options to there and that's it we just this is going to automatically um be created in our database so what we need to do it now is we need to initially say that one and the beginning or main window whenever it's actually get executed so something that we can actually do probably not the best way to do it but it's worse let's create a public boy let's call it a database function and in that one we just have a simple using bar dvd context that's going to be from our new tricolite db context and for that we need to import let me check the error yeah i need to import that one and for this new context the link is that i need to call is going to be my database and should create it and that will just verify that the database x is where it's supposed to be in order to call it after we initially said our component let's actually before that we just need to make sure that our database is centered how this one did like why didn't i get oh we lose this one over here so if we execute this now we will see that everything's run the same we actually don't see changes at all right let me just cancel it and let me open my solution explorer you will see well my database is not here yeah because the way that we do through through here the execution it doesn't happen in the root folder that would actually happen here let me show you click click here where it says show all ops files and the execution will happen actually here in bean so it will to be in debug dotnet five for windows in here we have a database equalize file and that's the one that was just created in order to show you that is the one that you've been created let's actually open our browser for sqlite let me open a database and that one is my user and source repos and we call navigation database example being the box.5 and we have the database equalized here and her categories and our products table so migrations are not working but we can actually see whether the categories have category id at the primary key and the controller name and our products has the produce id plug name and the category that one goes to the product's category category frankie you have a constraint reference to the category id itself so we thought that everything was created for us and the new indexes in there it was created automatically of course our database doesn't have anything but um we have the database just created well that's pretty much the way we want to have the database in this case in our main folder something that we can do is in the navigation solution we can do right click and we can actually open the file explorer or actually would copy full pad and after you copy the full pad let me remove the all files let me go back to my sqlite database context and i will add that full pad over here of course removing this the project solution if i save this in my case it's just going to point to that particular folder now i don't want it but you want to highlight that probably would be actually better for you if you do it like that so for now is let me use rubber back simplify stuff there you go so now we have that and we have that our table created we can actually start adding new elements some things that we can actually start doing and let's start working in adding new categories it's the simplest one so let's go to our solution let's go to the page categories page and actually for this one let's go to the page over here when i click at the category i want to add it so this text box let's put a name in there let's call this new category name and let's have a click on that click should do something doesn't give me the example oh click with lowercase l there you go new event handler and let's call this add category or now we can actually do here and let's rename this to a category and we can do a couple of things so let's essentially say is our database here of course for that one we need to import it and that was used right here on models there you go and now we can actually do a simple if statement and we can say new category name dot text is not known let's proceed right we're going to do and let's actually put that one inside here and we can use db context those categories dot add and we're going to add a new category and we need to send the category name equal to a new category name text now they look at their own text is going to be empty and we can actually then call the go back element let's say we can actually call it like this and now we can so let's just print this one and we should be able just to add a new category okay that should be more than enough the other things that we need to start doing actually skip like this so we should be able to go to categories and put it here for this category and i will add it and i put now second one and i will add it the first thing that we need to verify is that actually word is in our db browser let me open my database and i had it right here if i go to browse data my category is not there oh i know why it's not there i never save it so after i add the category i just need to save my changes then i just empty the textbook just in case and proceed let's see um something didn't like it do i miss it oh am i missing uh semicolon over here i don't know why execute let's call this one first let's call this one second if i try like this oh it works also it's not known it's empty so we need a way to do that checking but that will allow us to see here if i do refresh data we can see the first second and the third one that is actually empty is right there so we should we were able through the window to actually start saving an elements we learned that our text is not known so we just need to verify that this is not equal an empty string and that should be more than enough let me delete the correct record right and let me save write changes over here and that should be more than enough perfect now that we have that i want to see the categories that i already added in my application for that we need to go to the main page and we have that little combo box over here we should be able to show them over there so let's put this one here let's call the name let's call this cut list let's actually cut combo box main combo box because we have another one in the products one so let's call cat main combo box and we go to our main page css and here where we need to do that liking so let's put after unicode to be initially shaded we're going to create um what we call this connect to the database itself let's call thing database and let's create this method and for this screen database we need to use or [Music] same db context from our database let's import that one to order to see it and we just need to do something really simple let's do bar let's call this one how can we call it who can we call it actually i can actually let's call this we have our categories the context dot categories and we put it on to to list of type category so we read the database and we save if categories is not known we're going to our categories main combo box or cat actually main combobox dot is equal categories and we need just to select in this case and this is link you you need to select proper category use the category dot category this should be more than let's execute and this is going to go with go to see all my categories i will select the name and it will put us a source for my combo box so the window just opened and this combo box that i was empty for we had first and second right there pretty neat right we can go something here let's add i don't know third one and we have that category our combo box now have the third one array listed so that's the way that we can actually add categories let's in this case and i'm not deleting through the app any categories but do connection implement that as you want in my case i will delete the file mint book this one in order to create it again oh the other love me so let me actually change what it was supposed to do before let me copy the path let me remove the old files let me go to my context i will put my path right here and i will create a new empty database where i should not see any category anymore there you go this is empty a category a a is there let's add b c umd and all of those should be on the list so yeah it's working let's close this one perfect let's do something really really similar in our prod in our product page we have that one right here so this one let's call the combo box let's put the name category cv it's called pcb product combo box and because i'm being lazy let me just go to my main page let me copy this one forcing the database and i will put that one right here too the difference is yes i need to import my sqlite that's okay i'm going to import category and this one will be my products video and now these things database need to be executed after we initialized the product page that will allow us to see the same categories that we have here now it should be in the product page right here there you go and let's change the size of this when it's too small font size equal 24 align text alignment can another one no vertical alignment center there we go let's put the one through six then can we copy these two properties and let's put that one in the main page from my combo box there you go okay now it's more readable and that one should be oops in the product one there you go perfect so we're able to see the categories and we are able to do that now we should be able to add a new product and let's see how to do it because we are not saving on the combo combobot we are not saving the category itself as an object we're actually just getting the name so that means that we need to do some kind of lookup and it's not the best way probably we have like a list item with that object and the value and with the value will be the id we look for the ide stuff like that like something better but for now we are going to be looking for the um for the product itself for the category through the category name so let me go back to my product page cs here actually first and the add product let's create a click event new event handler let's call this one new bed handler there you go and let's call this one add product and let's go to the css this will need to change to a product and we just need to do a simple validation oops come aboard with a category and textbox let's change this one to be something we call this one new product name so we do a simple div statement and we say a for category pcb um select take index with a is not minus one i don't know that actually works let's see when is minus one that means that we can do a simple let's do a message bulk dot show and let's show the card pcb dot text there you go let's see if it has something we should be able to see the selection and we verifying with the selection is different than minus one when it's negative one that means they're not unique selected so here nothing do it but we select b we should have a message first with a b there you go and we go to main to empty it out it doesn't do it so that we are able to see the index of the selection so we should be able to do that again what else we need to see that new product name dot text is not empty too beautiful right it's not gonna do this no i can't and i have an issue what is this issue here fixed formatting what is complaining here i'll probably have an inch with my hand as simple as my hand it was not the correct one the method is not that that should work we shall lose it let me just copy that messagebox.show or new product name dot text so this is just a simple double validation we need to have something selected and the name need to be populated in order to be able to add it so let's verify if that's actually working now so this one does nothing if i select it should still do nothing until they put something here so we have the category and we have the product perfect now having that we should be able to working with our database so let's copy the code from our category here so we are going to do the db context right and then we need to look for the category and let's call a simple category cut equal database.categories where and here we actually have the link you for every category called keys where every c dot category name is going to be equal as the cut pcb dot text and we're going to get the first element yeah i know this is lazy but it's working and now we have the categories so now with the category the way that we had it we should be able now to do a simple db context dot product add and the product we're going to be adding actually we're going to get a category and then we do category dot product dot add so we can actually have the link in there and we have a new product with the product name equal this one and this one is without this okay now we do a d or or db context save changes to save everything and let's just preset this one will be equal mix one this one will be equal empty and we should be able to go to the main page right so we are doing is we are getting the category and see what hecate we are going to add to the products of that particular category the new product name that we just created and we save the changes of the database let's see if this actually works let's put over here select c first product we added and let's see our database and i have the file opened actually i really need to open it again because i change this will it locate it yes i select two one so i see my categories there are my products either with a category d3 because my category 83 you can see perfect so we'll be able to add the product the way that it's supposed to really so now we have a way to add categories and to add product and how to link those pro categories with the products we have almost everything done i want to do this a simple query now for the data grid to be populated with the product itself and let's do that and that will be really really simple it should be going to the main page let's add an element to the category to the category main combo box and let's call it whenever we select an item we need to do something oh whenever we click it selected value no how can we do it should we use the click or the change data context changed no i don't remember let me check my notes sorry for that let me check really quick my notes selection change it there you go okay yeah secure new and well let's call this one sync let's call this one thing um dar great dj um our dagger greatly we need to have a name and it's called an main diagram and object simple like that so i want to do whenever i select something i want to my datagrid to be populated and we call it a thing that agree method and that should be main page and let's call this one to think that there we go and the only thing that we need to do is something similar the way that we selected the category so we need to do um or category and let's call it cat just to simplify and actually we need to use the database first and let's put this one over here with the db context dot at all there is not we have to do the where probably category let's go with category.category name equal to my category pcba cb dot text i'm going to get the first element so we're going to bring the category and that will allow us to cr to get a list and it wouldn't be a list of product let's call this list please it's going to be a simple database oops what i'm doing let's call this pillis and it will be as simple as database.products database context actually i'm going to get the products i'm going to do a work statement for every product where product.category id is going to equal mycategory.com the one that we just pulled it up and we're going to convert this to a list and i wasn't mentioned before that is actually a list of product okay yeah as simple as that i really like how we can do different elements with or complicity in one line with link you either it's actually really neat so we can release from products but we're just getting those product where the category is the one that we selected in the combo box it's pretty much what we're saying here and with that we don't need to do or and how we call it or main data grid that item source equal to my list and that should be enough so that our grade should be empty most of the cases until we selected c here so we do a oh yeah oh what is issue here what is happening with the reception hmm let me see what's actually happening give me a second okay yeah probably here yeah the text is not the best way to get it we probably need to get the selected item and send us a string right so we have exactly the same type whenever contextual probably does what i was complaining about yes so let's try it again so we should be able to select yes and we can see that they actually get automatically the the different um columns so we selected several it doesn't change when we selected c we cannot see our first product was right there and yeah the category actually showed you different but i to go to the how to actually show the information they need to be there right we're going to do like a nesting and we need to change the template a little bit and i don't want to move forward with that much information i just wanted to see that the connection is working so let me add a couple of products some product here even if the second or another so we can have several products for every it was the random world let's put it on the second product you can see and let's give only one for the only one there you go so that will allow us to a to see two different products to be to see those three crazy random well or see we can actually see the list there and yeah everything is working we can add categories the lists are getting there we can uh to other and now we can list it perfect and we can navigate so this is a full-blown application and it connects to a database show me a page different pages and we can add elements and we cut out the different elements there i don't know go to details to the data grid i believe the last video actually do that or your book actually help you a little bit how to change the tags and the template for everyone and will allow you for the nested element how to actually export it and get the information that you need in this case probably the best course of action will be just get the category name or that category model the way that we had it right here other than that yeah i believe we have everything and we have it over yeah over an hour so it should be more than enough so i hope that you like it i hope that i were able to show you the process it was a little more doing messing around here i know it's not the best looking one but he did the job and we were able to connect to a database show different pages move away from here and there and it's a really simple quick example download it to understand that other than that i will say less the dropper any question any doubt don't hesitate to reach me happy coding everybody
Info
Channel: Learning with Rodo
Views: 401
Rating: undefined out of 5
Keywords:
Id: Jwkxxbc7BSw
Channel Id: undefined
Length: 53min 8sec (3188 seconds)
Published: Sun Jul 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.