Part 1 - Entity Framework Core with MongoDB.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to PRP coding YouTube channel I am PR ravindran today I'm going to discuss about the mongod DB provider for Entity framework code in this session I shall explain how to connect an asp.net 7 web API with mongodb using nty framework code so let's start I have created an asp.net 7 web API project and it is opened in my vs code editor in the solution you can see a models folder with a product class with ID name description and price as properties mongod DB provider for Entity framework core is now available in public preview to use this in our project we need to install a nuget package named mongod d. Entity framework core at the time of recording this video this net package is in pre-release mode to install this package right click on the project open in integrated terminal and run the command net package mongod db. NT framework code make sure that you have included the pre-release flag and click enter here you can see that the nuget packages got installed and let me click on this project and mongod d.t framework core with version 7.0.0 preview one is installed in this project the next step is to create a DB context class the DB context class is an integral part of the Entity framework an instance of DB context represents a session with the database which can be used to query and save instances of entities to a database so let me create a folder named uh context and inside the context folder let me create a class named uh DB context okay now this mongod DB context class needs to be inherited from the DB context class of microsoft. NT framework core for the DB context class to be able to do any useful work it needs an instance of the DB context options class so let me create a Constructor with DB context options as parameter DB context options of mongod DB context and name it as uh options and pass it to the DB context Constructor now the DB context options instance carries configuration information such as connection string database providers and so on the DB context class includes a generic DB set property for each entity in the model in our case we have a product model we will use the dbet properties to query and save instances of the product class so let me create a DB set property let me say like public DB set product and name it as products get yeah now let me close this terminal window in mongodb there is no table concept everything is a collection we need to explicitly map the entities with collections in mongodb this can be achieved by overriding the on model creating virtual method of the base DB context class so let me override override the on model creating method so it will create a model builder so here I can say model builder do entity set the entity as product dot to collection and I will give a collection name as products so the mongod DB context class is implemented now we need to register this mongod DB context in the services collection for that let me open the program.cs file and here we need to add builder. services do add DB context and specify the DB context as mongod DB context and pass the options as a Lambda expression say options dot I'm going to use the use mongodb function because here I'm using the mongodb provider now the use mongodb function is taking two parameters IM client and a database name and it has got one more overload is taking a connection string and a database name so I'm going to use this second overload so here we need to pass a connection string and a database name to pass the connection string make sure that your mongod DB is running in my case is I have installed mongodb in my machine and it is already running to test that let me open the mongod DB Compass client and here you can see that the connection string is mongodb Local Host port number 27017 so let me try to connect and here you can see that it is successfully connected and all the default database are shown here okay so now let me switch back to visual Studio code and as a first parameter let me pass the connection string so this is the connection string this is the this is the port number at which my mongod DV is running and the second parameter is I need to pass a database name so let me name it as products DB okay so fix the errors yes so now we have successfully registered the bongod DB context to the services collection now the next step step is to create the API endpoints for the read and write operations before doing that I would like to make a change in the product entity let me modify the data type of ID to object ID mongodb uses object IDs as the default value of ID field of each document which is generated during the creation of any document object ID is treated as the primary key within any mongodb collection it is a unique identifier for each document now let me open the program.cs file and add the code for the create and read API end points so let me replace this line of code with this here you can see this is the create endpoint and this is the read endpoint for this demo I'm using the asp.net 7 minimal API approach the create endpoint is a post method and it accepts two parameters as a first parameter I have injected the mongod context to perform the DB operations the second parameter is used to bind the request body parameters here you can see that I have added the product entity to the DB set and the context. safe changes assing method will make sure that the record is inserted into the mongodb database the read and point is a get method and it accepts one parameter mongodb context the context. products. tool list Asing will go and fetch all the records from the mongodb database now let me run this application and test these endpoints via Postman so let me run the application the application is building and it is running and here you can see that the application is running in port number 5,000 I have installed the postman extension for vs code so let me click on this icon and here you can see that I have created a postman collection named EF core hyphen hyphen API so if I expand this you can see there are two endpoints get all products and create product now let me click on this create product endpoint uh it's loading the request and here you can see that the URL is HTTP Local Host 5000 SL products it's a post method and uh let me click on this body and if I expand this here you can see that I have created a request body with name as iPhone 15 Pro description is Apple product and price is 1 lakh 59,2 okay so now let me click on the send request here you can see that the status is 200 now let me open the mongodb compass and refresh this page here you can see that it has created a products DB and if I expand it has created a collection named products and our data is successfully inserted into mongodb database now let me insert one more product item so let me switch back to visual studio code and let's make it as iPhone 15 Pro Max and give the price as 259,000 25 and let me click on this send here we can see the status is 200 let me switch back back to uh mongodb compass and let me reload this page and here you can see that the Apple product iPhone 15 Pro Max is inserted this time and the price is 259,000 25 now let me go and test the get all products end point so let me switch back to the visual studio code and click on the get all products end point and here you can see it is HTTP Local Host 5000 products and it is a get method now let me click on the send button and here you can see that the iPhone 15 Pro and the iPhone 15 Pro Max product is displayed now if you look at the AP response the ID shown over here and the ID shown in the mongodb combas is different in mongodb combas it's an object ID but in the APA response it is a full-blown object with some properties The Entity Framework core for mongodb has converted the actual object ID into this format it is better to show this ID inside the object ID as an API response for that we need to modify the get all products request in point so let me switch back to vs code and stop this server first okay let me click on the program.cs file and let's modify this ween point so let me add the code so here what I have done is I have created a new modified products collection with the only change in the ID here you can see that I have called the two string method so basically it Returns the string representation of the object ID and instead of this products let me copy this modified products and put it over here now let me run the application and test this end point and uh switch back to the get all products end point and click on this send button this time you can see that the ID is a string representation of object ID we have successfully connected mongodb with an asp.net web API using the new mongodb Entity framework n get package at the time of recording this video this net package is in preview mode this preview has a number of limitations at this time so finally we reached the end of this session if you find this video helpful then please like share and subscribe to my YouTube channel and stay tuned for the next updates thank you
Info
Channel: PRP Coding
Views: 3,143
Rating: undefined out of 5
Keywords: .net core rest api mongodb, .net core rest api tutorial | mongodb database 100, Minimal api dotnet 7, PRPCoding, asp.net 6 rest api tutorial | mongodb database, dot net, entity framework core, entity framework in c#, minimal api asp.net core, minimal api c#, mongoDB, mongoDB in asp.net web api, mongodb in .net 7, mongodb with minimal api
Id: 4cFvzCFrm7A
Channel Id: undefined
Length: 12min 45sec (765 seconds)
Published: Sat Oct 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.