Asp.net Core MVC with MongoDB Database Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to digital tech join in this video i'm going to demonstrate to you how to use mongodb as your backend database to perform crude operation like create read update and delete so let's get started open users create project select asp.asp.net core web application click next enter the name of the project mongodb crude the path where you want to save it and click on create select asp.net core web app and click configure for https and then click on create project so the project files and libraries is unloaded right click on the project name click on manage nuget package and make sure that you install mango db.driver the first one official.net drivers for mongodb and the version is 2.13.2 click on install i accept say installation done also make sure that you select the design microsoft entity framework design and install it fine so now we have installed both these packages now in order to get started with this project i recommend everybody to watch my previous video where i have shown you how to install mongodb in your windows environment and i've also shown some of the commands like how to fetch create collection the database fashion update record so i strongly recommend that before you watch this video you go ahead and watch the video of installation of mongodb i have pasted the link in this description of this video so let's go ahead before we proceed let's start the mongodb engine so go to command prompt and and which will actually start the mongodb engine and it is listening on port 27017 so we are going to use this as a connection string so and i'll like to show you the database also which i've already created show dbs and i've created a database called mobile store and it has a collection called mobile device where we are going to store all the mobile device information so i'll show you that use mobile use mobile store and show collections collections and i have one collection called mobile device and currently this there are some records not fine so three records over here so we will fetch and display this record in our page in our mvc application as well as create and update and delete records too so first thing our engine is started and now we go ahead and enter the connection string information in app settings.json file close curly brackets and then connection string colon and then we enter db colon slash local localhost slash sorry locals colon27017 then comma and we also mentioned the database name so database name as i mentioned it is mobile store comma so this is the listening port so as i showed you 127.0.0.1 is nothing but the localhost listing on port 27017 and this is a mongodb the name of the service so numerous service localnos and the port so we define the connection string now let's go ahead and add the the settings so we are going to create a folder called data which will actually hold the setting of the connection string as well as the db context class required to connect database and perform the code operation so let's name it as data now first thing is we are going to create a class called settings and settings will hold the connection string as well as the database link so so we define properties string connection string and then properties for holding the database name so our class setting is ready with these two parameters what we'll do is then we'll go to startup and we are going to inject this information in configure service method so we say we say services dot configure settings we add the reference of this class setting which we just added settings options open and close curly brackets and we say options dot connection string because two we get the connection string from configuration class file from the adjacent file dot get section and we mentioned the key name that is mongodb and connection string dot value so we define this key name in our app just json files mongodb dot connection name similarly we pull in the database name so options dot database because configuration dot gets configuration sorry see kept configuration dot get section and then we say db colon database and then we say value semicolon in the last is configuration get section yes it worked so we have done we have loaded that in our configure service method now let's go ahead and create the data model so we go to the models folder and add a class called mobile device entity the mobile device entity will hold the properties defined in our collection class which is which is just name company color and cost so we start defining the properties so let's say prop tap tap string underscore id now this underscore id is represents the mongodb auto generated id okay and define the name company color device color and sorry this should be double because this will hold the cost of the device and now in order to generate this object auto generate number while inserting a recording in the mongodb collision we need to add certain mango db class properties to this id property so we say asm id and we pull in the required i thought that is serialization and then is a representation and needs some sort reason type dot object id so this will make sure that whenever the records are inserted in mongodb collection id object as automatically generated in the mongodb collection class so we have defined our properties entity model now let's go ahead and create interface that will add folder interface so we are going to define an interface which will have the collection declared as well as i have all the required methods to perform the code operations so we say right click and say add say add new item and say interface select interface and i say i mobile store now this will be a public interface and it will have the following declaration the first declaration will be the will represent the collection defined in the mongodb database that means the collection name is mobile device so it will hold the collection so we say i collection and we call them mobile device entity class model and we name it as mobile device collection and this will be get we pull in the liquid reference and for i also collection we give this class as fungal so we have defined this now let's go ahead and define other methods required the first is getting all the information of device i enable and we pass the mobile entity and we say get all mobile get all mobile devices this is the first one then in order to fit particular information so we say get mobile we say get get mobile device details and we pass in the name of the device then we say create so we say void create and we pass the mobile device entity and mobile device data similarly we pass in we create for update here we are going to pass the underscore id and then the entity model and finally we defined delete operation and we are going to pass in the name so we have declared the methods and properties in the interface now let's go ahead and create the actual class which will implement all these methods so we go ahead and we had a class called mobile store contacts db contest let's call it as db context now this will obviously in it i mobile what was the name i mobile store we put in the reference using mongodb dot interface okay now we need to implement this method so we just click over here and say cool so fine so these already methods are declared over here now we need to do the actual implementation before we start doing that let's let's create a db object of database and create a constructor for mobile so db contest which will actually create a connection to our mongodb database mobile store so we say public only i db database and underscore db now we create a constructor class we have to pass this we are going to use a setting class so here i am going to call the i options and person as in the setting class options use extension options now inside the constructor we define a client new congo client congo client and say options dot value dot connection string similarly we pass in the database name so we say client once the client object is created we say client get database and then we are going to use database name defined in the database so our constructor will initiate the connection to the database and now we actually start implementing the first method is to this one to hold the collection object from the mongodb database so we are going to delete this and we are going to say underscore db dot get collection and and refer the name as defining mongodb database so we are going to pull in the collection object from our collection defined in our mongodb database and hold it in this variable called mobile device collection now using this mobile device connection we are going to implement our various operations so the first one is create get all mobile devices so let's get it to the top after our collection is defined okay now we are going to simply say return this is a collection which is holding our data return dot find a i use a lambda function a lambda through a expression means fetch all the collections and so this will return all the records that is all the records these records will be written and displayed in our view so this is done second is second is to get specific mobile information for a single record so we say after this method we say get mobile device details here we are going to use variable var mobile device details then copy this and then say find use lambda function to find m dot name equals to equals to name which is passed in a parameter and then we are going to say that first default done so so this will fetch the single return now obviously we need to return back so we say return the variable object that is holding this record so this is done now we go to create so we'll finish the entire implementation and then we'll go to our controller and view and create the object so now we are going to create it so create is more simpler it is a one-liner code okay so we are going to say mobile device collection which is this object and say dot insert one and mobile just showing review instead of single record and then we say update so we call the update method here there are a few lines of code first we are going to filter the record filter the record based on id and then pass in the updated object which uh uses a change in the view so for that we use the filters by device entity dot filter dot equals to id so this will fetch the record in the filter and store it in the filter object now we are using filing the update object command so builders [Music] mobile entity dot update and we are setting the parameters so first is name and then mobile data dot name we will use the same one we'll just copy for company for color and for cost so we are saying name then company will change it to company then color and then cost and semicolon and then mobile device collection dot update one and we are going to pass the filter and the update updated record so this finishes our update command and finally we have delete so for delete we are going to use the same filter but instead of id we are going to use name because we are passing in the names we copy name and paste it over here and then paste it over here and mobile collection dot delete one and filter all right so so guys we have completed our implementation so this is a core class mobile store contents which defines the database connection stream which creates a connection to our mongodb database then fetches the collection object that is mobile device creates a public variable and then uses the same variable to perform to fetch all the records get single record create a new record update a record and then delete the record now let's go ahead and create the views and controller to display and see things in action so we are back over here we right click on the controller and create a new controller called the mobile device we select an empty controller mobile device so we have added a controller the first thing we are going to do is set reference interface that is imobile store interface which is over here we are not going to directly reference our class option which is a good practice so we are back in our controller and we refer private read only i mobile store underscore okay we pull in the interface reference over here and now we create a constructor and we will pass in this create constructor so this will pass the i mobile store in our constructor of class now the contest underscore is ready to use it so the first thing we are going to do is that fetch all the records so we are going to use the default index one and we are going to say return view black context dot get all mobile device so this is done now let's go ahead and create the view we say add view we select the result view we select a template as list and we select the model which is mobile device entity yes our our view is ready now so you go on the view folder then there's a folder created called mobile device because we have created a control as mobile device and inside that right now we have only single view as index so guys let's go ahead and see things in action we are expecting that we should be able to see all these three records in our view so let's go ahead and do that before we do that we go to the shared folder of the view and click on underscore layout and here we select the home instead of home controller we say mobile device and let the action be as it is as index because our action is as indexed to fetch all the records let's execute okay so our page is loaded now let's click on home okay unable to resolve mobile okay enable resource service okay so i got why this error is there because we have defined the interface and we have defined actual class but we have not used our startup class and we have not told a startup class to use these classes in a project so in order to do that we need to inject use dependency injection and say add transit and we call i mobile store and the actual improvement i mobiles i mobile what was the class name i it's mobile store db practice and we pull in this got it so this was important we so now the project knows that it has to use the interface and the actual implementation class now let's run the project let's click on home great fantastic so we were able to successfully connect to our mongodb database and fetch all the records and displayed in our view so right now let's go ahead and quickly finish this functionality of creating new array delete and details so what we're going to do is that we'll finish all all implementation together and then we will see things in action so let's go ahead now let's create action for creating a new record in our mongodb database so say public i action result say create and this will return return view let's go ahead and create add a view razer view and select create make sure that you select the entity model so our create view is ready now when we post it once you enter and post it we want to make sure that the records are saved in the in our database so for that we are going to create http post and this will be create and post and this will take a method parameter as mobile mobile device entity mobile data we need to pull in the model object and this fill actually call the contest start create which we created and pass in the mobile data and then return redirect to action index make sure that you copy this create post go to your create view and in your form asp iphone action enter creator so once the user fills on the information and click on sub submit it call this method and add the record so we have done with the create routine now let's go ahead and implement edit so we copy the same two methods with some changes the first is we change this to edit and we pass in the name of a mobile device and this will be get and we need to pull in the information so we are saying on the so contest dot get mobile device details so define everywhere mobile details and then we passing the parameter and then we return the view so for edit we quickly create a we add a view we say edit and we select the model class entity and now similarly once you edit it you will post the record okay so so i think there is we need to just finish this up edit post and now we'll create edit and model entity okay now here we make sure that it is action as edit post and we write the code for that go ahead and edit post now in edit post what we are going to do is that we are also taking the id so here underscore it will be called it will be please note it will be http post and then it is update and it will pass in the id and the variable mobile data and written back to index so this is a routine for edit and finally we will do the details so for details i think so it will be get and we select the same as edit and this time it could be details we pass in the name and we fetch in the data it's the same routine we right click and say add laser view and we select details and model as [Music] so even our details is ready and the last implementation the last code which we need to write is a delete so we will have delete which will fetch fetch the information which will be similar to our edit or details or similar to our details and then we delete post so we say delete which will actually first fetch the information and then we'll have delete post should actually delete the record from our mobile db database which will be delete post this takes name as a string so here you can see delete and then it will return back to our index page so guys we have created all the necessary views uh action method in the controllers so you go to a view and click on mobile device then we can see it has index edit details it does not delete so we need to add the view for the delete so first is delete click on right click add view click on reserve and select delete entity okay so now we have index we should list the record edit selected details will show the details delete delete the record created create a record so i guess vr project is ready now i think we should go ahead and run it [Music] you click on home you are able to see the record first thing we are going to create a new record so [Music] company so we were able to add a new record and i think it should be able we should be able to see in our yes we are able to see let's go ahead and see the details we click on the details oh so we are not able to see the details so let's go to our controller details and let's put up okay so i understood maybe the variables are not having the values we'll go to index and make sure that you remove this and pass in the names so values were not getting passed because this was by default commented now let's run it so we click on home and we click on details and yes the name is passed and we are able to see the details also now we will see whether we are able to edit the record or delete a record so we click on delete yes okay so delete page is not created it is created but i think so we are not when you click on the delete the delete post is not having the value so delete action delete is not delete its action delete post okay and this will also need to pass in the variable so we we are going to pass the area variable so we are going to add input type hidden and you could type it in and the name which needs to be deleted so we are able to see the record we click on delete we click on delete yes we are able to delete the record also so now we are able to see the details we go back we are able to delete it and we are able to create it the final thing we need to do is we need we need to able to edit it so click on samsung a9 and we just name it and click on save so yes it is got saved okay so we can click on edit we can change the color and cost also and click on save so it got saved so guys i hope you really like this video i was able to successfully use mongodb as your backup database create fetch the records create new records edit records delete and even show the details uh i hope you really like this video if you didn't like this video kindly please request everybody to subscribe my channel to view more such videos and also like my videos so that it motivate us uh to create and work harder so that we can reach more audience and create videos which will really help them in their personal growth also as well as in their professional career also thanks a lot for watching this video thank you
Info
Channel: Digital TECHJOINT
Views: 248
Rating: undefined out of 5
Keywords: asp.net core, asp.net 5, asp.net mvc, mongodb, asp.net mvc and mongoDB, mongo queries, mongodb commands, mongo tutorial, mongo download, asp.net core mvc, asp.net core tutorial, asp.net core crud, asp.net core mongodb dbcontext, .net core mongodb example, asp.net core with mongodb, asp.net core 3 with mvc and mongodb, asp.net core mongodb tutorial, visual studio 2019, .net 5, create a asp.net core web application
Id: nJHQqH7PLMw
Channel Id: undefined
Length: 43min 47sec (2627 seconds)
Published: Sat Oct 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.