Get started with ENTITY FRAMEWORK in C#!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks my name is Yannick and welcome to tutorial to you and in this video I want to give you a brief introduction into Microsoft's Entity framework now I want to keep it as short as possible because your time is valuable so let's get started by the way [Music] first of all we need to install two packages Entity framework or and Entity framework core dot in memory so for this scenario right here for this test project we don't want to use a real SQL database this is why we use an in-memory database so as soon as we start the application a small database will get created in our memory and as soon as we shut down the application the database is gone right so but everything what we're doing is really it's very very simple to configure with the real SQL database and we also have a video on that I will just get back to it later so install both of them if you want to follow along now inside of my program it's just a simple console application.net 7 I have a class which is called purchase that purchase object contains an ID a product string and a decimal for the price now this should be a table in our database right just think of it like an SQL database and you have a table with all of your purchases and then you have three columns ID product and price right so what is Entity framework doing just let's just sum it up real quick Entity framework is an orm an object relational mapper and basically what it is doing is like it takes away the headache that we get from writing and connecting to an SQL database from and in c-sharp so we don't have to write down queries we don't have to create any kind of SQL connection using a SQL client or whatever we can simply install Entity framework and let the magic happen because from that purchase class right here Entity framework will automatically generate tables we don't have to care about the database structure inside of our SQL server for example at all we don't have to create even that database that will get automatically created the tables will get updated and patched and all of that right so we can really simply run and execute some simple c-sharp code and everything what's related to the SQL database to the mongodb database or to whatever a database for this example the in-memory one will get automatically patched so this is what Entity framework brings to the table and by the way if you haven't subscribed to our Channel please do it right now because you don't want to miss any of our upcoming c-sharp and net related videos which help you become a better developer earning more money and getting the job done in a better way and if you're really serious about your career and about your programming expertise check out our c-sharp progress Academy it teaches you full Stacks c-sharp web development using aspened core angular unit testing and c-shock design patterns definitely the best way to progress as a seashore developer check it out right now you can find the link in the description below or popping up at the top right corner right now and yeah there is for sure 100 and yes for sure there is a 100 money back guarantee for 14 days so give it a try now we already installed both of the tools that we need entity formal core and Entity framework core in memory so what should be your natural Next Step well first of all you should as I said create a class for your table so if you want to have a table inside of your database which contains cards you would create a class car you would add an ID for primary key for example you would add like a wheel count you would add like a brand or whatever maybe an age whatever you're interested in right and then you create a class now from that class Entity framework will create entities for that we will need a so-called DB context so if we install the tools we will get access to Entity framework core and in that way we also get access to the DB context class so what I want to do is I want to create a custom database context which contains all the tables that I want to have in my future well database so what I'm going to do is I want to create a public class I want to call it my DB context you can call it however you like and it's inherited from the DB context that's very important as you can see just in a second that we now automatically added Microsoft Entity framework core right here DB context now think about what tables you want to have for me I have a purchase class I want to have a purchase table containing all of the purchase records in my well which I want to save right so I'm going to create a property here let's write down prop tab tab the class that we want to use for that is called DB set let's add a type type of purchase and I want to give it a name usually you want to call it like your entity or like your class like purchase and then purchase this because it's a table and it contains multiple records so not car cars or house houses person people and I guess you get the idea right so public database said this creates a table in our upcoming database now there's one more thing that we have to do and that's very important so pay attention now we need to configure our database provider and this one differs depending on your future database type so there is a method that we can overwrite so write down protected override void on configuring and here we can provide a DB context options Builder there we go options Builder that's pretty default stuff and the important part is inside here so let's simply overwrite it right no worries about that inside here we can now configure a database provider so if you want to use an SQL server for example you would go to your nuget packages here you would search for Microsoft dot Entity framework core I just want to show it to you let's search for that Entity framework sorry and you can see SQL Server here if we search for that you can install SQL server and in that way you will get access to the message that you need to configure an SQL server in that scenario you would be able to call the options Builder dot use SQL Server now for me that's not possible because I want to use an in-memory database but if you install that package you would be able to configure an SQL Server Connection here simply write down the connection string and now as I already said we already have a video on that which is popping up right now this is how you can create and connect to a database in asp.net core anyways ASP net core for sure is also using the Entity framework right so this is why it's a natural like it's a good next step to to watch this video anyway we want to use an in-memory database because we don't want to be dependent on any SQL provider right now I didn't want to set up any a real database so we're going to use the in-memory database which is also fine for testing purposes and all of that so options Builder dot use in-memory database that's the method that we want to call open up the parentheses and inside here we can just provide the database name so the name that we want to have for our database let's simply say our database is called like my DB so nothing really special going on awesome so this is very important this configures our database provider and you have to do that if you don't do it you will run into an exception basically that exceptional error will also tell you like please go ahead and configure a database provider but yeah that's how you do it awesome so now we want to start using our database let's create a scope let's write down using inside here we can create a VAR context and we create a new mydb context so our database context that we have created right here right now inside of that scope we can now use the context which is well our connection or well it's like an abstraction of our SQL or in-memory database or whatever which we can now use so we can modify that context which is like our database and once we call Save changes on that context we will save entities in our database save changes in our database delete records in our database and all of that I will get to that now in a second if you are using asp.net core for example you would create that database context in your services so that they are for sure set up when your application starts and then well you can use the database the entire time but for this video where I only want to show you how to use Entity framework without HP net core and all of that just in a plain c-sharp console application we need to create a scope so that we have a scope for the database context for sure great let's create a purchase which we want to add to our database now so VAR purchase equals to new purchase there we go let's create an ID we can simply say just for testing purposes I set it to one then we got product name let's say this is shoes and then we have a price as a decimal let's say 4995 M there we go so we now have our product so we now have created our dotnet object purchase here now we wanna save it and for sure this is our table TV set we can put purchase objects into our purchase table so we simply call our context and inside here we will now have the tables so I can simply search for purchases there we go you can see it's a DB set purchase right it's just that object here so we're gonna say purchase dot add so now I can call the Entity framework methods to run our Cloud operations for example I can simply add an object so add there we go and now as I said very important this is just node it's like a node and it will not be executed until you run context dot save changes which is basically like just imagine you have an any kind of program and you write down something and when you close the editor everything is gone so what are you doing you simply hit Ctrl s to save the changes and it stays saved this is basically what you have to do you have to do it this is not optional you have to do it all the changes are gone great so now we should have um a purchase object in our database so an entity so what I want to do is like I really just want to show you the opposite side I want to grab all objects of our all purchases for example from our database so I simply call the context.purchases which take a look here is again for sure all DB set and want to turn that to a list so give me all records from the database put it in the list let me set a breakpoint here and let's start the application so once the application starts a new scope will get created and inside of that scope we can access our contacts because we are creating a new context right then we add a purchase object to our table and then afterwards I just try to get all the information from that you can see count one let's just open up that object here and you can see id1 price 49.95 product shoes awesome so now you already have learned a lot of stuff naturally what you for sure want to know about is not only the ad method there are some couple of other methods which I want to show you right now but first of all take a look at that one this is how you can get all records you simply go to context into your table take that table and put it in a list or whatever you want to do to array and there's so much more you can also filter using wear method and all of that so let's get to that right now great so let's ignore that what we got up here let's just focus on on that methods that we can see here now and the first one I want to show you is for sure remove so you already know how to add a new purchase so for our removing we can simply go into the table and say remove and then we can remove the record so again we would write down purchase here if we know which object to remove same goes for update so if we say we want to update the purchase object let's say the price was wrong it wasn't 49 it was 89 I would simply get or grab that purchase object adjust it and then save it back so for that I would use the update method great so this is crot operations create read update and delete right now I want to show you two more which is first of the fourth so first of the fault and in that way we can write down the Lambda expression if you have no clue about Lambda check out the video which is showing up right now it's just four minutes it will help you understand Lambda and C sharp and what we want to do is we want to take every purchase just to take uh think about purchases a table with multiple records maybe even though we just have one right now but we could have like a thousand let's say our product that we're getting here or our purchase purchase is equals to p goes to P dot product for example it's the name of the product right and we want to select it by shoes so please give me the first item that you can find the first record in the database that you can find which has the product value set to shoes that one and I want to show you another one just keep that in mind it's as I said a brief introduction where now we can filter let's say where the product goes to P dot price is lower than for example 100m so give me all products when the price is lower than 100 and you can see we will get an I carryable right aquarable and afterwards we could turn that into a DOT list for example to array and all of that and again if you don't know the difference between an i queryable and an ie numberable we also have a video on that so as you can see we are really focusing on a lot of specific.net content which should be perfect and very interesting for you now you don't have to name that P here right so this is just for me because I didn't want to call it purchase but I can call it purchase record because that one's coming from the database this one is a.net object and that one gets loaded from the database thread so please give me all purchase records where the price is lower than 100 so there are a lot of more methods which you want to use but that's well too much in detail to count and name them and explain them all but you should now have an amazing understanding of how you can get started with Entity framework core how it basically works and why it saves us a lot of headache because we don't have to write any SQL queries here right that where could be an SQL query but now I can simply wrap it up in a basic line of c-sharp code which is super easy for our c-shot developers right and this is why Entity framework is amazing thanks for watching definitely make sure to subscribe to our Channel like the video and if you want to see any videos on that channel any specific topics please write them down into the comments below and as I said earlier if you take your c-sharp query serious check out our c-sharp progress Academy great see you next time foreign [Music]
Info
Channel: tutorialsEU - C#
Views: 7,675
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, learn how to, how to, c#, .net, .net core, dotnet, core, code, asp, asp net, c sharp, coding, csharp, asp.net core, asp.net, programming, sql, crash course, dev, visual studio, database, mvc, entity framework, .net c#, efcore, c# ef, .net 6, c# entity framework, web development, ef, sql database, entity framework in c#, Get started with ENTITY FRAMEWORK in C#!, simplilearn, shorts, motivational video
Id: gQ2yBG3-jKI
Channel Id: undefined
Length: 15min 56sec (956 seconds)
Published: Thu Jan 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.