CREATE and CONNECT DATABASES in ASP.NET

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to tutorial at CU my name is Yannick and in this video I'm going to show you how you can create a database from scratch and how you connect it in asp.net core [Music] so first of all let's set up a brand new project I will simply type in asp.net right here and it will show up in asp.net core web application for this example we want to take that one because it really starts without any kind of authorization authentication or whatever and we can simply call it like create DB from scratch whatever you want to name it I will place it on the desktop next for the configuration I want to take a Net 7 project and I don't want to have any kind of authentication so that we really as I said start from scratch now I will just hit create just for your interest we also got the content of this video written as in article on our website at our blog right it's a step-by-step guide so if you want to follow along in the easiest way possible you can find the link for that article in the description below alrighty so our project is now successfully created and the first thing that we want to do is we want to install all necessary nuget packages so click on tools go to nuget package manager open up the manage nuget packages for solution window then click on browse on the top left corner don't click on install and let's search for Microsoft dot Entity framework core now scroll up until you really see Microsoft Entity framework core while my screen resolution is zoomed in so that you can follow along nicely but unfortunately every time I search something here it's like the first one is skipped and that's why I have to scroll up now click on that one select your project and click on install I need to scroll down for that but that should be visible for you simply accept it well this is really Entity framework core with all of the core features that we need as an object database mapper as you can see it right here but we're not done yet we need to create migrations and we need to have some tools provided from Entity framework so that we can really create the database and we'll execute our migrations I will come to migrations just in a second so what we want to do is search for tools so Microsoft Entity framework core dot tools go ahead and install that package too and afterwards we need to install one more which is really depending or it depends on whatever database you are using for most of you guys are using SQL Server which is fine right because it's like also installed once you install visual studio so what you want to do is you want to search for Microsoft Entity framework core and then we want to search for SQL server but there are also packages for like for example sqlite so if you don't use SQL server but sqlite postgresql or whatever go ahead and search for your specific database provider now for me it is SQL Server so I will just select that one scroll down again and hit install here so now we got all packages installed just in a second exactly what we need well at least the minimum stuff that we need to install to connect and create a database awesome so next up what we want to do is let me just first of all close that here and also that one we can we can save that doesn't really matter what we want to do is we want to create a brand new new folder we want to call that folder models because inside there we want to create our entity let's assume that we want to create a database and inside of that database we want to store students so in that scenario I want to right click on that model folder create a new c-sharp class I want to call it student and inside there I want to create well any kind of properties let's say three in total the first one will be prop tap tap int ID now the second one will be prop tap tap string name it doesn't really matter what properties you enter here because that's just for demonstration the third one will be prop tap tap we make that a date time and we say something like enrollment date so enrollment date there we go awesome so that's our entity we want to save students looking like that into our database so in the end the SQL database will have a table called students we will have inside of that table we will have a column called ID which will be our primary key we will have a column for the name and we will have a column for the daytime so this is why Entity framework is called an object relational mapper right now this is just a C sharp class right so right now there's really nothing going on regarding any kind of SQL well database creation so what we want to do is we want to create a database context our own database context so inside of the model folder again create a new c-sharp class and there is a base class which we will use for inheritance it's called DB context so as I said we use it for inheritance our own database context where we really specify what we want to have in our database we will call it a school like that and then contacts awesome so let's create that class and as I said the first thing that we want to do is we want to inherit it from DB context now for sure we have to use the Microsoft Entity framework core namespace this is why we installed the nuget package at the very beginning and now we are good to go to configure that context but before we do that let me tell you that we have an awesome course for you it's called the c-sharp progress Academy and it teaches you everything you need to know about c-sharp to land a job right so we turn you into a drop ready developer helping you to really land your first job take a look at the path that we have designed right here so you are like a c-sharp developer maybe you have some basic season knowledge inside of the seizure progress Academy the course you will learn asp.net angular restful API development Entity framework SQL software architecture unit testing and we will even provide you some training on how you can really get started in your c-sharp career that's really awesome so go ahead you can find the link for that in the top right corner popping up right now or in the description below so check it out I can really highly recommend it and I'm absolutely sure that this is the fastest and best way also the most affordable one how you can progress as a c-shot developer alright now let's start configuring our context here the first thing that we want to do is we want to add another property now inside of our context we want to specify what tables we want to have in our database we have an entity called student so in our database we want to have a table called students right this is where we go ahead public int and we change in to student so our entity class and by the way if you like this video go ahead give it a thumb up and subscribe to our Channel you don't want to miss any upcoming videos if you are seashup interested and then we want to name that one students now that itself for sure is like this one is a plural so we have multiple students and student here is a single class so that does not work we need any kind of collection right because we will have many students not only one and for that we don't use a list we don't use an array or dictionary no we use a class called DB set just like that and then we add the type so this is how you can set up or Define um tables in your database right in your database context DB said then you provide the type student and then you give it a name students usually you want to do it like not the singular not student you want to do it like the plural still dance because you have multiple students in your database now there are many ways on how you can continue configuring that and I decided to stick it with the well easiest one so what we do is we create a Constructor here write down ctor now we got this cool context we don't have to provide anything inside that Constructor but we have have to provide the options DB context options there we go let's call them options we have to provide those for our base context so DB context here so what you want to do is you want to pass that into the base Constructor this is required if you do not add that one well Visual Studio will tell you to do so right so just for that kind of configuration when the DB context options you will see where they're coming from just in a second because now we have to set up our service which well acts as our SQL database provider or well in total database provider so what we want to do now is we want to close that here because we're done with that again if you want to copy that code just go click on that article copy that code try it on your own and we want to go to our program.cs now let me just close that window here so that we have a little bit more space we are not interested in the app once it's built now in that moment we can well use routing map Razer pages and all of that what we want to do is we want to modify or we want to add a service before the application is built you can see in line 12 here the application is built before the application is built we can add services so as you can see here we got Builder Services dot add Razer pages so what we want to do is we want to go ahead and call Builder dot services and we also want to add something and we want to add well our database connection right so or well the database context to be more specific on that there is a method for it it's called at DB context you can see we need to specify type well good that we have just created our own context right so let's just add the school context right here and now we have to configure it a little bit more so open up parentheses let's create an object an options object there we go and we want to configure it and inside here we want to say options dot use SQL Server and inside of that method we only have that use SQL Server because we installed our SQL Server nuget package right Microsoft Entity framework core SQL Server that's the nougat package that you need to get access to this specific method so use SQL server and now we simply have to add a connection string that's all we got to do how do you get that connection string usually if you for example start in MVC application that uses authentication you will see in the app settings Json you will see your connection string here but right now since we have no authentication it's a clean from scratch web app we don't have a connection Stream So if you have a database provider installed like for example SQL Server chances are that you also have SQL Server management studio so there is a way to find out the connection string for every database provider now for me you can see that if I want to connect to something I can click on connect object Explorer and this is my my server name right here so I'm going to copy that just like that and then I simply go into the use SQL server and inside here I want to write down my connection string well to be more specific I for sure already created it so I would just copy paste it over and then explain what exactly I did alrighty so here's my connection string and it says server right there are so many templates out there for connection strings so don't worry too much on that server equals to now this is my server right it's my local server here and then we got a database School DB now this one is not created so I don't have a database right now inside of This Server so if I update that you can see database is still empty so there is no database yet and you don't need to create it because Entity framework will create it automatically just in a second here there is a little mistake there we go database School DB so I provided that name and then I added trusted connection and in my specific scenario I need to make sure that I have trust certificate set to true too but that's just because of my specific um SQL Server configuration so this is the only thing that you really have to make sure to check up on your own how to set the um the connection string but that should not be too hard because well you will always get any kind of information just in a second if your SQL server connection string is working or not once we start setting up migrations and trying to run them which we will do right now so we now set up our database provider we said we want to use an SQL server and we provided a connection string which is pointing to an SQL Server so that's perfect now we're nearly done Click On Tools go to nuget package manager package manager console and let's add a migration so a migration is like well if you are familiar with GitHub or git in total it's like a comet right so you make changes to a database and you want to keep track of it and then you can run it so a migration itself is the way you describe how a database should change but the migration itself does not change it you have to run the migration right so at migration let's call it initial create let's hit enter let's see if we are able to create a migration things are looking good right now so build starting that might take a while and once done you can see build succeeded and it will open up a fire a migration file in a second there we go on the right side you can see new folder called migrations and here you can see an up method and a down method I don't want to jump too much into detail if you are interested in that please just write it down into the comments below I'm happy to create an entire video just about well on the topic migrations but just to keep it very short the up method is like you run that migration how should the database change and the down method is like you can revert those changes so you keep track of what do I need to remove so migration basically consists of the changes that you make to a database hey add something or if you removed something for example it will also be mentioned here so that you can really like um using that migration rebuild the structure of the database once you switch the system for example you can also downgrade the database again well downgrade it's not the core word but you can revert the changes awesome now last thing that we need to do that was set our migrations we want to go to the package manager console again now that we have our migration I'm sorry I will just open it up one more time we will now run that migration and that will create a table inside of our SQL database it will name the table students and it will add columns for ID name and enrollment date so now you should get the idea of what a migration is doing right now we still do not have a database so if I update here and go to databases you can see it's empty but now let's execute all unexecuted migrations so let's just call update database that's another comment and now all migrations which are left open right so which you haven't executed yet will now get executed what happens now in that moment is right now we will try to make a connection and our system will see where there is no database so it will create a database now and then it will patch the database using the information provided inside of the migration so you can now see inside of my connection string trust certificate as I've got it right here trust certificate is an unknown keyword but that's not a big thing so I would just adjust it right now because the correct keyword is trust server certificate there we go now let's edit let's rerun the update database command and if everything works we can see a database afterwards in a simple small word which says done so let's just wait it creates all the stuff you can see that it's looking great and now you can see done and this is what you want to see right so if I now go to our database right here database you can see our school DB if I open that up you can see the table and here inside the table just takes a while you can see our students table you got the migration history and inside the students table we can see the columns ID name and enrollment date and now you're good to go to well use your database right so from here on it's more like uh take the context buy a dependency injection into any controller into any whatever you got right and then just Place new objects basically into the school context students right this is Entity framework so what you want to do is like students dot add write or insert or whatever system you created for your well crud operations so yeah that's it basically this is how you can create and connect a database in asp.net core entirely from scratch so thanks for watching make sure to subscribe to our channel so that you no longer miss any videos give this video a thumb up and well I know it does not stop right go into the comment section if you want to see any specific video on c-sharp ASP or whatever you can dream of just write it down I'm super happy to create any video for you definitely make sure to check out our c-sharp progress Academy is popping up at the top right corner right now or you can find the link in the description below you should definitely check it out trust me we have had so many students become better developers land their job and so much more it's definitely worth the look so see you next time thanks for watching [Music]
Info
Channel: tutorialsEU - C#
Views: 30,866
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, visual studio, c#, .net, .net core, dotnet, visual studio 2019, core, code, asp, asp net, c sharp, coding, linq, csharp, database, sql server, asp.net core, asp.net database connection, asp.net, excel, sql, mysql, programming, sql tutorial for beginners, sql tutorial, mvc, microsoft sql server, web api, create a sql database in asp.net core!
Id: ZX12X-ALwGA
Channel Id: undefined
Length: 18min 10sec (1090 seconds)
Published: Fri Jan 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.