EF Core 6 🚀 Database First / DB First (Entity Framework Core 6 / .NET 6)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends patrick god here welcome to this little entity framework chord tutorial thanks for dropping by and you know it already it's about the database first approach some of you wanted to see this and i gotta say this is actually done pretty quick so i assume you already got your database maybe sql server sqlite whatever it is and then you want to get these tables into your web api for instance so you want to create the models the corresponding models to your tables and now the question is how is this done how can you do this and again this is done pretty pretty fast it's pretty easy and simple but the big question is what are you going to do next so we will cover this in this tutorial and i would say we just start but maybe before if you like this tutorial and learn something i would really appreciate it if you click the like button maybe even subscribe to my channel don't forget to hit the bell icon to get a notification for new videos also please consider subscribing to my newsletter because then you get these kind of videos here earlier in your inbox and also access to upcoming online courses for instance the dotnet web developer bootcamp where we cover the backend part with the web api the frontend part with blazerweb assembly and also some other stuff like git scrum agile web development so stuff like that if you're interested then please consider subscribing to my newsletter thank you very much for that links are in the video description of course as always and the last thing today it's tea again but your support helps me to stay awake as you can see here this is the baby phone and it is green and no no blue bar if if you would hear some sounds then you would see a blue bar but there is no blue bar this means my little boy is asleep and by the way he's two years old now crazy right so two years on this planet in these crazy times but we are all healthy we are all well so this is great and now i stopped talking enjoy the tutorial all right let's start this time with sql server management studio because i want to show you how to do this database first approach with a sql server database and as you can see here i've got this blazer ecommerce database and i just assume that you also already have a database or at least have a sql script with this thing you can create then a new database with tables and so on because otherwise why would you want to watch a video about the database first approach right well maybe just out of curiosity anyways we need a database first so this is the one this is the blazer e-commerce database of my blazer e-commerce course in essence maybe you want to have a look and here you can see that we have several tables already addresses card items in essence it starts with the users right they've got an id email and so on and then also the products are pretty important i guess with the title description and so on so this is lots and lots of stuff the orders images categories and so on and now we wanna use the database a first approach of entity framework core to get these models here in our web api for instance and then we will also add one or two controller methods to well get some products or create some products something like that right so we've got our database here and everything is empty this is important to note i guess because when you have a look here then you see we have no users we have also um well we have products so it's not totally true what i told you we have products because these are seeded with this database so this is actually pretty nice because with that we can already create a controller method again to um yeah to get these products then after we created these models here in our project so long story short let's create a new web api project with visual studio 2022 there it is already asp.net core web api we click next and let's call this ef core db first tutorial great name dot net six it is configured for https yes you wanna use controllers not a minimal api the traditional controllers and also open api support for swagger ui so we can test this thing let's create it all right and when this is done of course we see the default weather forecast controller and the model and so on but now we can actually already start with the ef core stuff but before we can do that we need two new get packages we can either install them with the package manager console or you choose the package then you get package manager so right click the project and then manage you get packages and in here in the browse tab the first thing we need is entity framework and it's not server the first we need also the sql server provider but first i want to install the tools so that's the package we need entity framework tools for the new get package manager console in visual studio so please install this thing [Music] click ok and i accept and then you need the provider for your database and in my case it's the sql server there's also lights and you see it here other databases as well so sql server in this specific case it is we click install ok and i accept and when this is done we can close this open the package manager console let's have a look where are we well let's change the directory to ef core db first tutorial and now we are in the project and now we can use the scaffold db context command so scaffold and then db context it is and now you already need the connection string so can be tricky at times but if you're using sql express for instance like i do it in my case then you enter server sql express then the database name in our case that will be blazer ecommerce and then also trusted connection to true so this is then the connection string then we need the provider for our database which is then microsoft microsoft entity framework core and then sql server and then also an output directory let's just say we call this models all right so scaffold db context and then in quotation marks the complete connection string with server sql express database is blazer ecommerce in my case trust the connection set to true then the provider microsoft entity framework or sql server and then as a parameter the output directory in our case let's just say let's just say this is then the models directory doesn't have to exist and now the whole thing is building the build is succeeded and yes we get some warnings so this is well how it's done with the db first approach what does it say sensitive information regarding the connection string all right normally it would be mapped to a non-another bool property but it has default constraint to another bullet property to allow difference between setting the properties of okay so i am pretty sure if you already have a database that is bigger then there might be some stuff where you just have to pay attention and then have a look in essence if everything works as expected but the great thing now here is you can already see it here we've got our db context right so this is the thing we can use to access our models and or the entities the tables and also the data of course we have our connection string here on configuring um is is generated for us and also on model creating with an index and foreign keys as you can see here a user has one address we've got the a combined key for the card item and so on so lots of stuff that entity framework creates for us these are here these are the warnings we just saw i guess and that should be it in essence now the next thing we should do or maybe we should have a look at the models first let's just have a look at the user model for instance we've got an id email password hash password salt date created the role and then a virtual property because of the relationship so we've got also our address and now let's have a quick look here there's the user id email password hash password solved date created and roll and then when we have a look at the address for instance we've got the id and the user id and all the other stuff and now back to visual studio we see here the user as a virtual property again and also the user id right so when we now get our uh well information in essence from the database with the controller for instance we will do that in a second and then we might be able to also get the user or the address depends on what we want to get first of the specific entity so i would say we do that but first the next step really important is we have to register the dbcontext this is not done automatically so with builder services and then add db a context we get our blazer ecommerce context or register this thing and we have to add using directive and that would be using ef core db first tutorial models that's the one and let's just make this global so everyone knows about that okay and now i would say we create a controller for our products because we already have some products and we also have virtual virtual properties here like the category for instance and we have a look at our database again we see products here category id here and then our categories and let's have a look here also some data in here as well so maybe we can do something with that without the need to add new data in essence so let's let's add a product controller i would say just for testing purposes it is an empty api controller and we call this now product controller we add the constructor because in here now we inject our blazer ecommerce context so blaze the ecommerce contexts call this context and initialize this fields i like to add the underscore here all right and now simply a get method so http get it is and now here we say public async task i action results let's call this get products and in here now we simply say return okay and then wait context products to list to list asic all right and we need ef core for that and i also want to make this a global and if you want of course you can also move this to the program cs or another file where you just add these global usings so maybe it's a bit better organized more organized than in this case okay i would say we test this now it is time and let's just see how the result looks there we are we've got our product if you're wondering why we don't see the schema here the the actual product class or the model it's because of this thing here we just return an extra result but we can change this real quick in essence by doing it like that so we specify really what we are returning and yes we want to rebuild and apply the changes and now we see a bit more stuff right so now we see lots and lots of stuff that is uh well used in the products itself so we see the product model we see the images order items the product type the product variant and so on okay so now we try this we hit execute and we get a list of products isn't that nice now the problem is well it's not really a problem but there are uh well how can i say this you see the images are empty or items product variants and then we have a look at the database again let's see what about the images first right so let's close the addresses do we have images no these are not all right but what about the product variants now oh there are product variants why don't we see any product variants here well there's an easy fix to that we can simply say include and then a lumped expression for every product please also include the product variants and maybe we put this in a new line and also this save it it is rebuilt i guess try it out hit execute and we get an error a possible object cycle was detected well this is totally true because now when we have a look at the product variant we also have a virtual property here for the product so when we get the products and we include the product variants we get the product variant and this thing also wants to include the product again so then again we come back here and want to you know you i think you get the idea so what we can do is simply say json ignore as an attribute add the using directive with ctrl period or the click just click on the light bulb for the quick fix menu and with this attribute now we should not get the products but let's let's have a look okay try this out hit execute hey it works nice so now we've got the product variance as well and this is now the thing you have to consider right so db first is great you you get your models pretty fast but now you have to change the model here in in a way where you have to add this attribute for instance or the totally different way would be and maybe the better way better practice in essence is to create dtos data transfer objects where you know how the specific object you want to get from the web api looks like so if you want to i don't know create a product dto for instance then you would use the id the title description and so on but maybe you don't need the product variant here or you use another product variant dto where you then have an object with the product variant information but not with the product because you know that you don't need it because the thing is when don't know how you build your project but if you're changing the database with sql server management studio or in any other way and not in the code first way then you you need the models again right you have to generate the models and we can test this actually if i think you you know what i want to say if you change something and you then run the scaffolding again then your changes that you did here in the code are gone so let's let's just have a look again well or it says we cannot create it use the flag force okay so we use the flag force and what happened now to our product variant well the json ignore is gone and you have to do the work again and again and again so you really have to pay attention here if you want to use the db first approach another way to do all that is to well you have your database i get it i totally get it trust me i was in that situation lots of times and you use the scaffolding but then after you did this it would be better than to use the code first approach right so you start an initial migration with the ef core code first migrations and then your changes are done here in the code and then you add another migration update the database and so on i've got weird videos about that when you want me to create a video specifically about the code first migration where you where you really focus on that stuff please tell me that in the comments and then i will do so but i've got the video about all relationships with entity framework core 6 in dot net 6 so there we already use the code first approach in essence in every other video where we use a database with entity framework core i'm using the code first approach and of course again for instance in the blaze ecommerce course or all my other courses it is code first i just think it is the better way to use ef core if you want to use ef core but if not then maybe you want to use depa hint here video about dapper is coming soon maybe i don't forget to add the info card here otherwise please have a look at my channel and don't forget to subscribe and like this video thank you very much for that okay so well this is not a big tutorial here talking a lot because it's it's more a question about the the concepts and the way to to create your application to build your application and you asked me in the comment section about the database first approach so this is how it's done it's really it's really not not much this is the important stuff here this is the command first not with the force flag but you need the scaffold db context connection string and the output directory and you need the entity from chord tools and then the database provider there's also another way to do it not with scaffold db context you can also use dotnet ef and then the and then db context so maybe we can have a look here dot net ef right and then you've got db context so let's have a look here dot net ef and then db context help and you see that scaffold right so what we can do now is db context dbcontext.net ef db context scaffold and what about that well we can define what is it well we've got the force flag again we've got the output directory and yeah we've got the connection string again and also the provider to use so pretty similar to the scaffold db context command and it really looks pretty similar so again we need the connection string maybe i can copy this here so that would be this thing and let's just copy this and now with scaffold and then the connection string then the provider and here we can just say minus o models and i think we now get the arrow again because the models are already here already exists correct so let's add the force flag and wait a sec dash f and now it should work yeah all right so we get the warnings with the would normally be mapped to a non another property and so on but you get the idea again the same model so two ways to do that i hope you learned something and in essence that's already it this is how the database first approach works with entity framework core as i said pretty simple right you've got your scaffolding command you can use the.net cli if you want but the big question is when you well scaffolded the dbcontext when you got your models from your tables what are you going to do next do you want to change your database your table with uh secret server management studio for instance so really sticking to the database first approach or do you want to switch to the code and then use the code first approach where you then just add migrations and let entity framework core do its magic and change the tables it's totally up to you and i think you know that i am a fan of the code first approach definitely use i like to use migrations and yeah that's why i'm a big fan of entry framework in a sense but as i already uh said in the tutorial dapper is for instance also a great orm object relational mapper a tiny now what's it called a micro orm where we can of course also well run sql statements and then get the results as our models in our web api for instance but this is for another video if you want to see more depth then please again write it down in the comments and then i will do that of course for you guys so thanks for watching if this video resonated with you then please consider clicking the like button maybe subscribing to my channel clicking the bell icon of course also the newsletter maybe this is something for you for the upcoming web developer bootcamp and the last thing again thank you guys so much for all your support [Music] art great tea to calm down a little bit right so maybe you don't need coffee all the time and as you may be heard in the video i was really really really tired when i recorded this so yeah i definitely need more coffee all right so that's it i guess if you want to see more then check out these videos here on the site maybe there is something you like then click on this thing and enjoy this next tutorial and apart from that i can only say again thank you so much for your time thank you so much for watching and i hope i see you next time take care
Info
Channel: Patrick God
Views: 81,158
Rating: undefined out of 5
Keywords:
Id: 2bsRQIhtTxs
Channel Id: undefined
Length: 25min 30sec (1530 seconds)
Published: Tue May 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.