Create ASP.NET Core Web Application With SQL Server Database Connection and CRUD Operations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you guys and welcome to microcode so today we are going to begin a very interesting video whereby we are going to create an application a web application using asp.net core and SQL Server so if you have not installed Visual Studio you can actually if you have not installed Visual Studio you can actually refer to our previous videos for installation of Visual Studio 2022 so we are going to create an employee's management solution so we are going to create an employee management solution so I've just launched Visual Studio 2022 then I can create so on your navigation side you can see we have a section for create new project then you can search for asp.net then we are going to create an mvcsp.net core web application MVC so proceed to next then we can call these employees management then I can yeah maybe you can save it there and you're going to use a visual studio.net6 this is the long term support foreign tication leave it as none so this is our application it has actually created our base application so as you can see here we have our rww root with which hold the CSS and JS files we also have the controllers currently it has our home controller then where we will be placing our models and where we will be having our views so if we just launch our application yeah you can see we have our application as launched so we are going to create an our application using asp.net core and uh SQL so we can create a model so you can add a class here for employees so employees so here we can provide the attributes of an employee you can provide the primary key so this will be a primary key then an employee fastening last name last name email address then we can have the phone number so you can see these ones they're indicating if you want this to be nullable you can actually have a question mark in front of this you don't want them to be nullable so we leave it as it is so for the phone number you can have it as int so that is it so for us to interact with our database we love to install some nuggets so we'll be using the Entity Framework so we will actually share a video about Entity framework core so for us to install it you just click on the application then manage nugget packages then we can we can install entity just type in Entity Framework so you install Microsoft Entity framework core the latest stable version minus 7.00 so if you see this icon here it means it has installed so we want to install SQL because we will be linking our database also so search for Entity framework SQL Server delete this version then you want the Entity framework tools so we have tools here latest version and we want design Entity framework design so we accept so you will click on the installed so on the install nuggets you'll be able to see them here from this section and as well on the dependencies and the packages you will see them installed there so we have the same version so that is how you install the Entity framework core so we will want to create now our DB context which will be doing the manipulation of our database that is being all the operation uh crude operation of the database retrieving and adding adding data so for us to do that we'll create a DB context so you can create a folder where we want our DB context too so you can just call this contact so we'll be using Entity framework a code fast approach so we'll talk about this more on our next video about Entity framework so Entity framework actually is an object or relational map framework so we call it EF core in short so it makes database communication more fluent and easy so the database context class that you want to create acts as a database from a c-sharp code and it will contain all registered classes that will actually act as tables so we will create a class here called Deep DB context so DB contact so this DBA context will act as a communication between the c-sharp code and the database so since we have installed all the nuggets of the Entity Framework so we'll proceed and now create our DB context so to manage all the manage all the connection with the database we'll have so we can just create our a Constructor there then this one so you can just call these uh let's rename it to application DB context because it it needs to extend application DB so that is it it needs to extend the big context so this DB context actually is the one now using Entity framework so the Entity framework DB contacts need to be narrated by the application DB context to act as a database contact class so this will now act as a database context class so then on our Constructor will provide the DB context option just as a we Supply the DB context option then we provide our our class application DB context so that is it and remove this and have it easily foreign [Music] context options then you want to extend these two is a context option so these we are defining our so the DB context option is an instance of options that we are going to register them in our program.cs so this is how we are creating the so it will act as a con so that this one will now uh will be registering them on their program uh CS file so all our tables and classes we need to Define them here so since you have created our model which is the employees we can now uh register all our database classes with that DB set so that the Entity framework can communicate with the tables of the database so so we'll do this DB DB set so then we Supply our model here it is employee so I just done something so model should be in singular please so we just rename it to be employee great I like it when they are single then here we our table should be employees so with e has gaps so this is now our so we are using what we call code fast approach in Entity framework so you can recite this but you you can actually look for our videos for more of this so we'll be doing a video about Entity Framework call so we have actually defined one of the art so our table we expect our table to be called employees so what we need to do is actually register our database connection so how do we register that we register it on app settings Json so here we can Define connection stream we just do that it will actually Define then you can provide our default connection name so this one we are now supplying the server address of our database so I had installed SQL server on my machine and ssms that is a Microsoft SQL Server management Studio so I launch it so you can also download it is actually a free version and I'm using an Express Edition so I'll just collect so to get my server address I just click on properties and I have it here so I can copy it then come to my application so here I'll do this server then I paste it there so then after you supply the server you put in that semicolon then the database name [Music] so we need to create a database so just click to database new database then say employees or you can see you can call it employees it's fine employees click ok then we'll have our database here so we don't have any cable so far so we'll see how this is being uh how we will create the tables so copy the uh database name then paste it in front of the database then full column provide the user ID so my user id name is so I'll type in my name so you can also do the same after you have installed that then the password and also put in the password which is admin then I need to to put this other so there are some ssls Trust server certificate so that you don't get any trust issues so I'll just give this as is equals to true I think that is fine so after we have defined that we need to register our our so we need to register our DB context option on the program.cs so what we need to do will come here so I had done something let me just copy it so I'll just copy this so I'll come to program.cs then we register our DB context so we have our application a DB context then we are using SQL Server then configuration connection it will get our database connection from these uh screen that you have just defined now on the app settings so since we have done that and we have already provided our table so our next thing to do is to apply migrations so we want to effect the changes of the the to actually create the table the employee table this table on our database so to do that come to tools then nugget package manager then package manager console so you receive a window like this so here we can add migration just say add migration then provide the migration name so you'll do also migrations on our next video initial migration then press enter so what this will do it will actually create a query an SQL query that will create our table you can see create a table employee name then our our Fields these are our Fields first name middle name last name email address and the phone number and actually put in a constraint put in the ideas the primary key for our employee table so after you have done that to apply this migration to uh our database if we use update database command so if you press enter foreign [Music] Express database so and just click come here so our settings so I look on our settings SQL Server so let me just see what's going on so this is our server it's all right just initiated them I'll copy this here and replace with these so we have our server name these are server name database is employee employees then our user ID is that one and our password is other and then trusted certificate and allow multiple active connection is true so if we just try again it is done so you can see it has actually apply the migration so if we come to our our database and refresh it we expect to see the tables good so you can see we have the migrations table so when we apply any migrations so it will be tracking the history so you can see our migration name is initial migration and this is the version then this is our table employees table so you can see we have our ID first name middle name last name email and the phone number so we have not created this manually but through Entity framework called migration so we have done that which is good so our next process is to create our application so come into views right click it then add then we say new stuff folded item so this one will create our views and the controllers so choose the MVC controller with views using Entity Framework then click add then select our model class put his employee then our DB context which is employee application DB context then you can allow it to generate views and reference any script libraries and use our default layout page then you can also alter the controller name but for my case I leave it as it is so if I click add it will actually create the crude views that is the create index update View and the delete View so let's just wait so that is it so it has created our application so you can see we have the another folder here does introduce another folder called employees so if I I click on it you can see we have our view for creating deleting uh viewing details edit and the index which will give us the list of employees so we have a controller down here but I can I'm I may want to move it at the top the controllers folder so cut it and paste it there so yes so it will rename the spaces so if we launch our application yes so you can see it still we cannot access our employees uh where to create an employee so so what we need to do come come into your program.cs so you can see our default route so we change this to not to go to the home controller back to employees employees controller so you can so the name of the controller should start from the employees up to where the controller starts so that is how you get the name of the controller so if we Now launch our application we expect to see the employees section where we can create our employee good so you can see we have now the list the first name middle name last name email and the phone number so if we create a new just put in some names then phone number you will click on create there we are so you can see our employee details there if we come to our database and we just execute this we can see our data here so we have actually connected our application with SQL Server so proceed we can now try to tweak the application so if we want to change the namings we can come to layout you can come to layout then we space this then come to employees index so you can on the index you can say these employees management then you can see this add new employee so we may want to use a font awesome so for to add from person to our application come into our application then add then client-side Library then just type in here from provider will be CDN Js so type in phone toaster so e80s give us the latest uh version so you can just click install it will install to the lib folder so here we have so it has actually completed so we want to reference these two applications so we'll come to shared and we reference it here so I'll copy this so it is at leave so I'll just copy this name then paste it here then after font awesome we want to access it the CSS file I'll paste it there then I want these all CSS file and paste it there and I also want another one which is a font awesome so I'll take also these so you have a reference for awesome to our application so you go back to our index so we have just so we can have even an icon here so close that then I say class you can put in a phone to some icon it is it you can copy this one and three use it down here on our edit buttons so it can put in your pencil then on the details view this will will be an i on the delete these will be trash so I can also put in a class with the edit buttons so yeah Plus button class maybe again [Music] with this also button plus 18 you can delete and also have this class RTM danger so just just trying to make the application look more friendly so if I just now run this there you can see we have our application name so that is uh so you can add as many employees as we want so yeah museums I can add another one I will go to field data here so this is how you create an application in m sp.net core MVC so this is just a sample up so we'll be advancing this application on our next videos so you can actually now edit so you can see it has pre-populated our data and we can save it we can just put in here one two three phone you can see it has changed you can put in here zeros so we'll be doing validations on our next video you can put in here five six seven eight comment Down Below on your suggestions [Music] so that is uh so view so you can see it has our details you can go back to the list delete you can delete here so that you can see don't double so you can delete as well this one so so that is it guys for our application so comment Down Below in case you have any question or you recommend any session that you can have in the main type uh in the meantime uh actually I recommend you subscribe to our videos to get more insights and to get the notification whenever we post new videos thank you and see you in our next video
Info
Channel: Macro Code
Views: 4,599
Rating: undefined out of 5
Keywords: C#, AmigosCode, ComputerScience, Classes, .NET, .NETFramework, .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, .NET 7.0
Id: rTM6Q000cLw
Channel Id: undefined
Length: 27min 29sec (1649 seconds)
Published: Mon Dec 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.