How to use In-Memory database in Asp.Net Core App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good evening all right so uh in this session basically we will see how to use in-memory database with the airspeed.net core application so that is actually the main agenda for this session so the in java technology you have s2 database which can be used as an in-memory database so dotnet also there is an option right now available which is actually called uh entity framework called in memory database so we'll see how to use it in asp.net core application so i'm going to the visual studio here create a new project so i am going for asp.net core web api so this is the project template time series so since we are going to create a restful web service here so application name and give us memory demo then select a folder here i am using the latest version of dotnet that is dotnet 5.0 okay so other options are also available here i'm going for the latest version i don't have any authentication type here so i am fine with the https configuration i'm not going for docker because it's only a simple uh desktop application i'm creating or asp.net core application which is not going to be dockerized so i'm not enabling the docker here and creating the application okay the application is created now okay so this is actually the order structure of my application now uh the first step is the dependency we need to include so you can go to the tools you get package manager you get package manager you can go for install package and the package to be installed is microsoft dot entity dot in number so this is the package which you need to install okay so this is the easiest way to install you might be familiar with the installation of nuget packages from package manager from here also you can go here go for managing your package for solution here now if you see that's already installed you can go browse here and otherwise you can go here you can search for in-memory it will get okay so from here also you can install but since it's already installed you can see that uninstall option will be there so it is already installed through the package manager console now if you go and check the dependencies here the packages you can see that uh entity framework for memory database 5k has also been installed second step is basically to create a model so i'll create a folder here create a folder called model inside the model i'll create a class give the name as employee clear and i'll add a couple of properties for the employee hmm generally that so this is my model this is energy class okay so this is going to be my entity class so let me be there only so that's the second step first one is dependencies that is microsoft industry framework or in-memory dependency unity so second one is create models so how many other models you want you can have so we are here we are going with only one option and another thing i want to point out here is i am not going for any repository pattern or i am not going for any service layer or something like that since its only a demo on how to use in-memory database with the dotnet and directly try to access the data from the controller itself okay so i am not going to have a enterprise gate application with the dio layer service layer and process i am going to access directly from the controller so this just to understand how to use in memory database with the dot okay so second one is the option second step is also complete the third step is to create a db contest object so that also i'll create inside the model only if you want you can have a separate folder created for that here i am directly putting it to save some time so i'll give the name as employee db contest and this employee dba contest as we all know should be interpreting from db contest so i will uh include a constructor here which takes a db contest option it has options and i am just passing that particular parameter to the base class constructor okay then i'll define my db sets here fair enough so it's embroidery so that's all okay this is the third step you have to create the db contest object now you have to register the bb contest with the the startup it resists as a service with startup so i'll go to startup here the configure services method here i'll register it says service so before that you need to go to app setting so these are these all things i don't need i will delete this one whether forecast and all those is created by default i don't need that uh similarly uh the controller also the weather forecast controller right on it okay so uh app settings uh this is actually not required but since when we are working with a normal database we need to we usually define our connection strings here so i'll go for connection strings here i'll give the name as my db only this is the property name generally okay this is only a database name in memory there is no concept of any connection string so only the database name but i am showing here as a connection string so that the people who are not familiar with how to use connection strings in dot net core you'll be able to understand that so name also i am giving it as my dpoly this is a database name only this nothing nothing to do with the connection string or something like that in memory database we are only bothered about the database name we are not bothered about any connection strings or something like that okay so that we are done now we can go to the startup class here here we will register this one services dot add dt contest add db contest of employee db contest okay db contest options we need so i'll put it as option option dot use in memory database and this is the database name we need to mention here okay so instead of specifying the database name that you can directly give the database name here but i will read it from the upsetting.json file say configuration dot get connection string i'll specify the connection string here actually so this contains the data so that's done so that is the that's all so the step is done okay so that is only steps which are record now we need to go and create a controller so the controller folder i'll go and add a controller here going for api controller okay so here i will create a field here it's actually employing db counter story do the names underscore contest okay so this will be automatically dependency injected into the constructor and that is assigned to the context so dependency injection is in work here so since we have already registered here in the startup dot cs5 automatically when that constructor gets gets executed the dependency will be automatically injected now we will go to values controller so okay so this is looks like so we put it as employee control we should have changed the name there fine that's fair enough now we will go with the operations okay so first one i put it as public list of employee check it get employees input okay so here i specify return underscore contest dot employees dot to list and this is an http get method second one i'll go for get by id so that also i'll go public it's going to return an employee object and this again is an http get better but it takes one additional parameter okay so that idea mentioned here so here we will say return underscore contest dot employee is not simple or default we such that v dot id okay assuming that the uh id is already there assuming that it is there we are not going to type anything now okay so you can also check whether it is available or not but i am not checking here i will check somewhere down when we go for the put operation or delete operation will do so here let it be like this only to save some time and the next one is delete employee input okay so public here i will put i action result i'll put the name as delete into the id so this is again http delete operation here also will take one additional parameter which needs to be mentioned here ready okay so here we will check whether the id you are providing is already there so i will put r emp equal to underscore contest dot temporaries dot symbol or default xs that x dot id equal to equal to ready so meaningful name if you want you can put a meaningful name okay so i will put x is employ only here it is shown here because i am using resharper now i will check if emp equal to equal to now return not from it's not existing okay otherwise will return okay whatever you want there are two overloaded methods there yes you can have a lot of mothers are there you can pass a valley object value or simply so i will put a message here so i will say employee or you can uh delete an object also delete the employee itself you can return okay so that's also fine fair enough whatever so that is delete uh now you go for add operation post operation so that is actually uh http operation boy okay so we will add it as underscore contrast dot employees dot something we missed out here because when you delete okay we are not deleting it now so you have to delete first so how will you delete it you have to delete it first by issuing a command called uh underscore contest dot employees dot remove is the mother okay so to that i'll pass the located employer okay not only that after that you have to say underscore contest okay so that will do the things here we will say employee dot add add this employee not only that underscore contest dot show changes and you can return we want uh two not one created as the status report status code so i'll say creator here so created uh take a url string you are like specifically with us employees slash it's actually api or template api slash employee plus employee dot id comma this is the actual object that got added okay so that's fair enough i can complete it all now only update is spending public reaction result okay and not only passing object i am passing id also here so if you don't want to pass id you can directly pass employee object also but in the employee object you need to pass id also in that case so i am going for the normal conventional way of passing the id along with the update operation [Music] here also we will locate first so that this code remains the same that will check like this here we will check if employee dot name not equal to none okay if the employee name does not contain the object does not contain meaning in that case emp dot name equal to employee dot name similarly if copy paste this one gender right similarly h also if age is not passed by the user then the value of the edge will be 0 so employee dot h not equal to zero then b dot similarly for another one laundry okay so all the data updated the only thing is that we need to update for that actually contrast dot update so update expected entity as the parameter here so i'll pass emp here which contains the updated data wherever data comes in that will be updated if there is no data passed for any of this field like age or salary or gender that will be the old value which is taken from the database then save the changes okay then you can pass return the value okay you can specify employee plus id okay okay so i think we have done okay this is an update operation that has to be mentioned with the http put operation this also take one additional parameter as the path variable or parameter so that you mention that me just run it on and see whether there is any exception being generated no exception all the documentations are there okay so we go and specify this as slash api slash employees blank okay so you can go and add the data take this url from here and put it here okay get operation here also will result blank so i'll go for a post operation here go to head s raw data okay so ideas one i put okay some value post operation okay you can see status 201 created okay change created and the data is being returned also now you can go and get the data you'll get the same data if you specifically ask for one specific employee here for example one so going for two you can see here two not for no content okay so but that is not that there we have not written the uh that so we will try to delete two which is not there employee with id 2 does not exist there are methods will be shown but if you go and update we will go for an update operation do i say put employee with id2 does not exist so i'll try to update one yeah so i'll update one and change name to my verma and this i'll make it as 88 000 remaining values i am not passing ok only these values i am passing id also there is no need because i am passing through the path parameter passing as a power parameter only name and salary are the value i am updating here so i'm passing it it says employee with id 1 updated successfully and now if you go and try to pull the data out you can see that uh manuvarma and the salvi has been updated with 1800 okay so this is how uh you do in memory database how many ever you want you can add but if you want some initial data to be added okay before adding before post operation you need to have some initial data to be there in the in memory database that's also possible here so what you need to do here is you have to go to startup class here in the startup class i'll create a function in say public static void say c data and i will pass employee db contest as a parameter here with the name of contest okay then i'll create some objects here so i'll create a blowing emb1 equal to new of employee go for object initializer syntax id equal to one name equal to is salary equal to 45 000 this is one employee object second one is employee emp2 to ema is equal to 27 solid equal to okay now here we are getting contest object as a parameter here so uh i will add to that particular contest object contest dot employees dot add emd one that is the first employee contest dot employees dot add mp2 that's second employee and contest dot sages okay now come to this configure method this only for updating come to this configure method here i need to get a reference to the service employee db contest employee contest is already registered here as a service so you can get it from here so the configure method here i will say i can say what contrast equal to app app is actually this one i application builder here you can see that app dot application services dot get service and you can specify which is the service you want accept type parameter here okay so i'll say be honest so many of the documentation says only this much okay but this will not work i'll show you why it is not working yeah so uh okay fine uh i'll get the contrast now i can call this method see data and pass the call test object okay so uh save this one okay now if you try to execute this one you will get this error cannot resource of service in memory demo model employee db conducts from the root provider so this is the error will come so many of the documentation says only business but this is not working so what you need to do is you have to create a scope first so for that this one to create a scope equal to app dot application service dot ph this is the first step which you need after that you can create the contest you will get the contest you can say scope dot service provider dot get service here you specify what is the service you need employee db contest okay now try executing it once again no error here api slash employees will get the data clear so whatever data i have added here that is getting displayed here now you can go and check all operations here now here also if you issue a get operation here you can see the two data is there i am going for the post operation so i put class three here and sometimes i put it here okay so it says andrew comes here so you can get it you can see all the three data is there okay so you can go and update only name i'll pass remaining i don't want to change at all whatever you want to we have written the code in such a way that you don't need to specify anything else whatever you want to update only the data you pass okay so here i am try to update name only so anil is uh trying to convert you have to specify the this thing here so this is three here employee with id three updated successfully we'll see whether it is updated successfully go and get it you can see that this gun got updated okay similarly uh try to delete uh we have delete we have not tried previously so we will try with four here no does not exist so i'll try to delete to id to delete it successfully now if you go and try to get it object operation okay we have not written the code there that's right here so see i have written the code like that we are simply returning it we are not checking whether it is present or not that is why it is coming okay so if you try to delete this one you will get the error message because you have written the code if you go and check here you can see get by id you can see that we are not checking for any error condition directly you are trying to repeat that so that is why it is not showing in here but delete operation update operation works perfectly fine so to delete it and we will if you try to go and get all employees here you can see that one and three okay so this is how you use in-memory database with a.net application so that's all i want to put it across in this particular session thanks thanks for watching
Info
Channel: Reni Varghese
Views: 708
Rating: undefined out of 5
Keywords:
Id: EInLb2IiDao
Channel Id: undefined
Length: 40min 50sec (2450 seconds)
Published: Tue Aug 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.