CREATE ASP.NET CORE USING POSTGRESQL. Using PostgreSQL with .NET Core 6.0

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to macro code and today we are going to learn about sp.net core connection with a post agreed SQL so we are going to create a web application then we connect to a postgres SQL so we'll just launch new then you're going to create the speed.net core web app model view controller template then it will be a student's management system s management system then proceed so you're using.net6 then you create a new application so that is it so we have our program.cs file already created and we have our app settings.json so this is where we'll be storing our database connection then you have the um controllers views and the models so let's get our domain class for the student we can name it as student so make your domain class single so then we can define the properties here ID which will be our primary key so you can actually decorate this okay so then you can have a class name then middle name then last name and also have a full name but now this fully name will be a combination of it will be a combination of a first name middle name and last name so you will take the value of first name combine with the value of the middle name just copy foreign [Music] [Music] [Music] [Music] gender so this one allows the null so if you want it to be not to accept null values that I will declared now with that way in these are sorry this is fine so that is it so we can now Define our DB context which will act as a connection to our database so you can create a folder call it context when we Define here a class which is a which will actually derive from DB context it's like I need to call it application TV context so this is delivered it will be a class deriving from DB context so this one is actually from Entity framework course so we have not installed it so you can actually install it so it will install Entity framework core on our application so if you come to packages then you'll be able to see it you have the latest version so we need a we need uh our class DB context option so what you need to do is to Define our Constructor so public so I can copy this patient DB context and these will be the context options where you can just [Music] send this user this is our so what you need to do is to Define our database here so rdb sets you can have a model here students so that is it so we need to install now the nuggets for project scale so npgsql so we're going to install these MPG SQL Entity Framework postgre so I installed that nugget so it's already installed and also install the design Entity framework called design controls and design I can have this thing then so we have it so you can register our DB context on on our program.cs file so I have some I have some connections here so you can so we are installed an objects so what we need to do is to register it here so we'll come and register our application now I guess that so we'll say Builder inside the B context we'll be having RDP context so DBA context is application Maybe context sorry so that is our class so using zero registered fun options here where you can see options [Music] by using mpsql so you can you can now register these so that's fine so then configuration not get connection s yes connection string so we'll take our connection stream now gonna close these you can Define first our app settings so here you can say connection stream okay then here we can have a [Music] you can copy these default connection all right so this is our database connection so we'll be having this Avenue the port for postgres default Port the database that is the University then user IDs postgres and this is our password that is our password so we'll be using this default connection on our program.cs so we'll be having it here so this one is trying to so we are having these ones so what we need to do here is a builder that that is our you have registered our DB contest application context so you have also registered our database connection on these are the side so what we need to do is Now launch our first pgsql and create this database called University and see my password there password so we don't have the database you can create a database name University there we are so you can see our database doesn't have tables under schema then tables so you don't have any table so since we have done that next thing we need to do is to create so we have a model called student so we need to create migration so that this student table will be created on our database so what we need to do we apply migration come to tools then nugget package console then you'll have such a screen so I'll copy a command here so I'll explain these so we are using a command called add migration then initial migration you can call Initial migration then this is our application DB context then we want it to be enough data then under migrations so press enter so the the data the application DB context was not found so you can take yeah so the application DB concept textiles double P so you can rename this to be application DB context we can do this then we can also do it here so that is fine so we can do this again and so that's fine so you can see it has created uh an SQL query that is going to create our table call students and add these fields with a primary key ID which is auto generated so since we have since we are okay with these uh this statement so what we need to do is to update our database so we use update a database command so press enter so it will actually do that until it's done so one thing that you need to know is uh the only thing that will change when connecting SQL that is a Ms SQL and postgres is this service here and how you connect these two to the connection and the packet but that is the Nugget packages that you are using you see we are using npg SQL so that is the only difference so you can actually switch an application from using uh postgresql to mySQL or even to mssql so if we come to our database and refresh you can actually refresh you're able to see students and the migrations table this keeps tracks of the migrations that we have done just to execute this you can see we have this migration and and the version so our student table if we select so you can see we it has created our table ID first name middle name last name email address for number gender which is empty so you might want to create some students so we'll come to program.cs then under arm we redirect it to the students controller then then we launch our application all right [Music] from the report is being used let me try using uh is yeah so what you can do you can consider watching our videos on uh the Kestrel web server and the is server so if we come to the properties launch settings you can see we have these uh is settings and we have these uh Kestrel profile and the is profile so if we try launching these using the is let's just try again so the port is 443.82 so you can check on our ports foreign foreign context [Music] then so the controller you are looking at the controller yeah I think we are fine so uh we need to create crude operation so it is trying to so where I was there there is here so it is trying to find the student's controller and we don't have it so let's do the scaffolding of our students so click on views add so you can stop it fast add then controller and just click on views add then select new folded item then select MVC controller with views using Entity Framework then select the model class to damage them application DB context or application DB contact then the controller will come here so you can leave the control as it is then click ok so it will create the the normal views that is the create view update View and the index if there are students controller so there we are so you can see it has created students controller and you have the index which retrieves the student's data the details which actually gives us this student details creates The View and the posts so you can see we have it here so since you have done that you can see we have when you come to views we will have our controller here so you can move this controller you can just cut and paste it on the controllers yeah so we have it here so if we if we just launch our application we expect it to run there we are so you can see it is now running under 443.83 so since that was not the issue we can actually revert it back under launch settings.json divided to 82 so that is how you change the ports so I'm launching using the is Express website so that is it you can see the port is 443.82 so we can now create a student a student name we can call it GMS then test out gmail.com then this is b07 then this is now so if we do that then press create you can see we have now our our student you can edit female save seeds are updated you can add another one John mango Vincent okay gmail.com [Music] then mail so we have our data there so if we come to our database and the students table and you try to execute these you can see we have our data here so it is actually saving the data to our postgres SQL was gray SQL so that is how you you create a crude operation using postgresql so if you come here then we delete one of the record so you can see that it's fine so if we run this again one student is gone so so that is majorly how you create a cool operation using asp.net core SQL so guys remember to subscribe to our videos and share comment uh comment down below what you want us to do on our next video see you and uh keep us safe thank you
Info
Channel: Macro Code
Views: 31,894
Rating: undefined out of 5
Keywords: C#, AmigosCode, Classes, .NET, .NETCORE, Sundeep, Saradhi, freecodeCamp, ProgrammingWithMosh, MikeDane, IamTimCorey, TeddySmith, FireShip, DaniKrossing, Stefan Mischook, ASP.NET CORE, Coder Foundry, Jose Montemagno, .NET 7, .NET 6, Visual Studio, Frameworks, Nick, Nickchapsas, Jake Wright, Keep On Coding, QuestPond, Comrades Flavour, Nicholas Kioko, Amigos, Developer Filip, TechWorld With Nana, Techworld, GotoConferences, CRUD, ASP.NETCORE, SQL, SERVER, BoostMyTool, KESTERLWEBSERVER, WEBSERVER, IIS, PostgreSQL
Id: SlYf25tCCYY
Channel Id: undefined
Length: 22min 43sec (1363 seconds)
Published: Mon Dec 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.