Entity Framework Core Tutorial - Everything You Need to Know to Get Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone today's video is all about Entity framework core what we're going to do is we're going to take a crud-based rest API that's currently working with storing the entities in memory in a dictionary and instead of that use Entity framework core this video is going to cover everything that you need to know to get started with entity frame record today what the different nuget packages do how to use the.net CLI tool to work with entity firmware core and also various other things that you should know if you're just getting started with Entity framework core today if you're new to the channel then first of all welcome my name is namikai I do have a quick disclaimer that even though I work at Microsoft I'm not talking on behalf of Microsoft and also if you're watching this and you're not subscribed yet then make sure to pause the video smash that subscribe button if you don't want to miss out on videos about coding architecture and various other coding related stuff that you might enjoy so what we're going to use for today's video is the Uber breakfast application if you haven't watched the video where we build this crowd-based rested API in one hour then I'll link it over here make sure to check it out if that's something that you care about what it currently does is like we said it works with an in memory dictionary of entities and we're going to use Entity framework core instead so to get started all you need to do is clone that GitHub repository it's public on my GitHub so all you need to do is say git clone and then select the Uber breakfast repository click this this will clone the application and now we can open it in Visual Studio code okay so I have our project open in Visual Studio code and if you just want to catch up and understand what we did in the previous video then you can simply look at the readme file that has here everything that you need to know to get started now right so what the different endpoints look like what requests you need to make and so on and if you want to play around with it a bit then you can open the requests folder which has here the various requests and you can simply run the project make requests and see how it behaves so to get started we have the two projects the Uber breakfast project that has all our logic and and the contracts project for our API definition then we have our breakfast service that contains the actual logic of storing the breakfast in the in memory dictionary of breakfast now what we're currently doing is we simply have this static dictionary and we're manipulating this dictionary to store the breakfast locally in memory right so we're doing it over here and we're doing it everywhere else now let's switch this to work with Entity framework so the first thing we want to do is we want to add the main Entity framework core package which is simply.net ADD and then we need to select the project in our case it's the Uber breakfast project and the package that we want to add is simply Microsoft dot Entity framework core and that's it let's run this this will add the nougat package to our CS project okay now that we have that let's go back to our service and create here a new folder let's call it persistence and inside here let's create our DB context now this video is suited for complete beginners so don't worry you're going to understand everything that we're presenting today we'll go back to the theory in a few minutes so over here let's create the DB context so let's give it a unique name for our project that's called Uber breakfast DB context okay so that's a public class Uber breakfast in DB context and this is going to implement the DB context from entity thermal core that we just added all right and now there are a few things that we need to do first of all we need to create a Constructor that will call the base Constructor in the DB context so let's accept this delete what we don't need and let's walk through what we have so this the DB set we can look at it like a list of breakfasts and that's from our point of view we're manipulating it as if it's simply sitting in memory okay now of course this isn't what actually happens behind the scenes we have some database provider that converts whatever actions that we do on this DB set to whatever database provider that we're working with so this might be SQL this might be API calls and it's very important to understand what actually happens behind the scenes okay so that's the DB set and over here we're simply calling the base conductor and passing it the options over here maybe let's put it on a new line so it's visible and that's everything we're going to have for now great so we have our DB context now let's manipulate the breakfast service to satisfy what we're doing over there but instead of using the in-memory dictionary like we're doing over here we'll use NC from recording so let's close this we have a bit more room let's get rid of this and instead let's say we're here private read-only this is going to be the Uber breakfast DB context yes the second option and let's call it simply DB context let's create a Constructor and pass it via dependency injection so we have our DB context now let's use it instead of the dictionary so we have over here the DB context over here what we want to do is we want to add it to the database so let's say the big context and let's say simply add and let's pass it the breakfast because of course this isn't enough we haven't stored anything in the database yet what we want to do is call Save changes that will actually execute the command whether it's SQL where whether it's an API call whether it's something against a documentdb or a relational DB we don't know yet at this stage we'll get to that in a few minutes so let's say over here deep context and save changes that will actually store the breakfast in the database so we have that let's move on to the lead breakfast okay so over here we have some ID what we're going to do for now this isn't the most optimal we're going to check if it exists in the database if it doesn't exist will return not found from this method otherwise we will delete it so let's say over here breakfast yes and then we want to find the breakfast by the ID this will return the breakfast so over here we are executing some command to retrieve the details from the database then over here we want to say that if the breakfast is null then we don't have a breakfast the find method will return null if there's no breakfast then we can say over here return errors.breakfast.not found and this will return that error otherwise we know we have the breakfast over here and we can simply call remove so now we can say DB context and then remove and pass it the breakfast the next thing you want to do is again call Save changes for it to actually be persisted in the database so the context and save changes and now it's actually stored in the database and we can return deleted over here great moving on to get breakfast then very similar to what we did before so what we want to see over here is DB context and we want to say breakfast then we want to find by the ID if this is a breakfast breakfast then we simply want to return it over here otherwise we want to return not found all right moving on to absurd breakfast so what we have over here is the following we're returning error or absolute breakfast this is a response that we created and this absurd breakfast has the is newly created Property that indicates whether we just created a new breakfast or not that's because the HTTP specification for put is that we return 201 created if we just created a new breakfast when we called the put method or we turn 204 no content if we observe the breakfast if we made some changes to an existing resource so we have this and this is so we know to return the appropriate response in the controller so we have this what we want to do over here is we want to check if the breakfast exists in the database if it exists then we want to update it and return this with its newly created true otherwise we want to join with false so let's say over here let's get rid of this entire thing and let's see over here the context and let's say breakfast and let's try finding the breakfast with a given ID and if it is not a breakfast that's a baby breakfast so we know it doesn't exist in the database and now we are creating eight new brackets okay now that we have this then we can say if it is newly created then we can add the breakfast to the database otherwise we simply want to update the breakfast so now we can say DB context and then breakfast and now we can say add and pass it the breakfast same thing like before in the create breakfast method otherwise let's say that we want to do the same thing but we want to update the breakfast last thing we want to do is we want to call over here save changes that will actually persist the changes that we did and the last thing we want to do is return that value of the is newly created so we know to return the correct status from the controller now if you're familiar with entity from a core then there are a few things over here that first of all won't work aren't efficient and can be done better so we'll talk about these as the video goes on camera go to the Uber breakfast project is the Young Brother of the Uber dinner project the advanced Series where we're building a rest API completely from scratch using clean architecture and domain driven design what this is now is just to get you up to speed with everything you need to know to get started today and use Entity framework core in simple crowd-based applications but using LED framework core together with domain driven design is tricky there are a lot of things that you need to know especially when it comes to configuring and mapping and so on so I wanted to tackle the simple things now so you're up to speed and you can join for the advanced topics that we're going to talk about later so now that we have this then like we said there are things here that aren't going to work but we still have some things to learn ahead of us so the next thing we want to do is we want to Define what our database provider is because at the moment we have our rdb context we haven't added it to the dependency injection obviously container yet nothing is wired together so let's go ahead and do that so let's go to the program CS where we're setting up our services and over here let's say Builder dot services and we want to add the DB context the DB context that we want to add is the boober breakfast DB context and let's accept this and let's get rid of all of this for now because I want us to explain it as we go on so this will add it to the dependency injection LC container but we need to Define what the database provider is so for that what we want to do is we want to take over here the options and use it to configure what it is now if you aren't using asp.net core then this exists also inside this object so over here you have a method that's called on configuring that you can call so you can simply say over here override on configuring and then over here you have the DB context options Builder that is the same object that we're using over here okay so I want us to visualize it so for that let's take a look again at our diagram we have the boober breakfast client looks something like this and it's trying to create a new breakfast so it's calling the post endpoint and it's passing the details of the breakfast then this arrives at our backend service and currently what we're doing is we're simply storing everything over here inside the breakfast service but like we said we don't like that we want to use the DB context instead now the DB context or empty framework in general it's a layer of abstraction above the database provider which is actually the implementation that will store things in whatever database that you're using now out of the box if you're using asp.net core then you get the in memory database that you can use and simply Define that that's the database provider that you're going to use with your DB context we'll look at that in a minute but what we're going to do today is we're going to use sqlite this will create a local file that the data will be stored in but there are many providers for basically any database that you want to use over here I put just some of them but there are more other than this and it's important to understand that when you're working with the DB context then you're working with a layer of abstraction that is a heavy abstraction where you're simply using link and the actual action that happens behind the scenes varies based on a provider and based on the provider that you choose you need to understand what is actually created when you're creating migrations what the migrations look like so we'll look at that in a minute but I just want to emphasize it again and again as we go on because again this is an introductory video and these are things that are important to know when starting to work with entity thermal core so great we have that out of the way it's visualized we know exactly what's going on so let's go back to the project and over here let's say options and let's say which database provider we want to use so over here yes we can say use sqlite and this will use the SQL Lite database provider but as you're probably familiar with the.net ecosystem then things are broken up into nougat packages so you don't include symbols and things that you don't need so for that we need to add another nougat package that is for the specific provider based on the provider that you're using you'll need to add a different Nuka package so let's say over here dotnet ad and then yes this and over here let's say we want the SQL like provider this will add that nougat package to our application and now we're good to go so we have this configured let's try creating our first migration if you're not familiar with migrations then all it is is the definition of how you're going to update your database every time you update your database then you create a new migration and that's the definition of when you move from one version to that of the database schema to the next version then what that conversion is going to look like both when you're going up to the next version and when you're going down to the previous version so let's go ahead and try to create a migration for that we're going to use the.net Entity framework core tool for the command line so let's go ahead and edit you can check if you have it installed by saying.net Tool then you can list all the tools that you have installed globally and in my case I already have it installed so you can see it over here okay now every.net CLI tool that starts with dotnet and then a dash then you can either invoke it like it says over here with the short name this tool is has all my various socials so if you want to install it and you're curious what my social is for let's say GitHub then you can run this and you'll get it but that's for another day so you can simply write either the short name to invoke it like we just did with the am or you can say dot net and then EF and that's what we're going to do so to create a new aggression what you want to do is you want to say.net and then migrations then you want to say the migration name in our case it's going to be yes initial create and you need to specify the project if the project doesn't exist in the folder that you're in so in our case yes we want to use the Uber breakfast project you can either write it explicitly like we did over here or you can simply say Dash p as well so let's do that and over here let's say Uber request okay so doing this we'll try to create a migration let's see if this works okay so it didn't work and that's because when the entity firmware command line tool tried to build the project and create the migration definition we'll look at that in a few minutes then it uses the microsoft.nt thermal core dot design new package and we don't have it installed so let's go ahead and add it to our project as well so say.net add and then we want to say to the Uber breakfast we want to add the package and then we want this thing but in the end we want design so let's invoke this this will add that nougat package to our application and then we can try again okay so it finished adding it let's go ahead and try running it again and let's see if it works okay so while this is building I just want to recap so we added three nougat packages all together we added the base entity forever core package which is Microsoft dot Entity framework then we added the design package and that's for building the migration or building the definition of the migration then we added the SQL Lite database provider because that's the database provider that we decided to use and that contains not only the logic of the provider but also this extension method on the options Builder that allows us to use it as our database provider for our DB context okay so we did this and now we see that we have a failure and the failure is no suitable Constructor was found for any type of breakfast so let's look at our breakfast and let's look at our Constructor definition and try to understand why this doesn't work so looking at our The Constructor definition then we can see it's private and we have over here many arguments to the Constructor and this doesn't work because what happens is the twin antifremacore tries to materialize the breakfast object based in our case we have the table so we have columns and rows when it tries to take the data that's stored over there and materialize it into a breakfast object then it tries to use some Constructor and it doesn't know how to do it with this given Constructor so what you'll see many times in projects that use antifremacore is the following you'll simply see private and then breakfast and this is going to be used by antifremacore when it creates a new breakfast object now you might be asking yourself how does it work if it's private and the answer is that with anti-firmware core many of the things happen with reflection so it's going to use reflection to actually populate the breakfast object another thing that we're going to have to do and let's get it ready out of the way is to add private Setters for all these properties because when it's going to add values to these properties and also when it builds the migration definition then it's going to look at this object or look at the class definition then iterate over all the properties and use that to create the definition of the database so let's go ahead and change everywhere where it says get to be instead get and then private set right great so we have that out of the way and now a different core should have what it wanted so let's try running this again and see if it works okay so we ran it again and it didn't work and if we look at the failure then we can see that the entity type list of string requires a primary key to be defined so like we see over here we have here our two list of strings one of them is for the Savory items one of them is for the sweet items and what happened is that we have here a one-to-many relationship so we have many Savory items for every breakfast same thing goes for the suite and we need to decide how this is going to be stored in the database so we can either decide that we want a table and the items to be stored over there or we can do something else and basically Define how the conversion to the database is going to look like and the conversion from the database is going to look like and then use that instead so for that let's go ahead and do the following so inside the DB context class let's go ahead and use the on model creating method two customize how we're going to configure our objects so we have the on model creating yes let's accept this and now we can use the model builder to decide what our database is going to look like so what the name of the table is what the columns are going to look like we can add here different columns we can remove columns we can ignore properties and so on there's many things that we can do over here and this is the place to do it so we have the model builder and what we want to make changes to the breakfast type then we can say over here entity and then select yeah for example the breakfast type and over here we have has key but we can also say stuff about the different properties and configure them now because sometimes you're going to have only one DB set but sometimes you're going to have a lot or more than one and also sometimes the objects are going to be very big and you're going to have a lot of configurations over here then it can become a mess doing all these configurations over here so instead of that we're going to take a different approach which is create over here a new folder and let's call this folder configurations and inside here let's create our breakfast configurations and the way this is going to look is let's say public class and over here we want the breakfast configurations and this is going to implement the I entity type configuration testing for the type breakfast and then if we implement this interface then we see we have here the entity type Builder which if we're looking back at what we had before then when we call this then we get the exact same thing which is the entity type Builder so it's just a cleaner way of doing the same thing okay so let's go back to our breakfast configuration and put all the breakfast related configurations over here now let's open on the right the breakfast definition just so we have a reminder of what we have so we have the ID the name description then we have three date times and then we have these two lists so we failed on the list so let's start with that and so we're able to see something let me close this and let's make this a bit smaller okay so we have here the Builder now let's go ahead and configure how these Savory and sweet items are going to be converted when we're storing them in the database so what we want to do is the following we want to say Builder and then we can say property and select the property that we want to configure in our case we want to change the Savory items configuration and we can say over here how we're going to store it in the database so what to say is we it has a conversion that's on the way in it's going to have the following conversion yes as GitHub is suggesting so on the way in we're going to take all the values and concatenate them with commas in between then on the way out we're going to split it and store it that way great so we have the conversion for the Savory item let's do the same thing for the sweet item let's let GitHub co-pilot write it for us and great so we have that now other things that we might want to do is we might want to cap the length of the strings so by default when it's simply a string like this then it uses the maximum value for the length of the string so let's go ahead and say over here Builder and then let's select the property that we want in our case we want the name writes as a b and then we have name then over here let's say has next length and over here we can say the maximum length in our case we can say that we have here at the maximum values that are 50 for the name so let's say over here let's use it for now you can do whatever you want of course this is just an example so let's say over here Max name length and that will be the maximum length for this let's do the same thing for the description so let's say description and then see over here next description length and that's the definition for two of these then we have here the ID that we are generating the value so by default it lets the database generate the value for the ID so what antifremacore doesn't is it has a lot of conventions for various properties so for example if the name of the property is ID then it knows that it's the ID for that entity Now by default it tries to generate a value for that ID and because we're generating it ourselves so if we go back to our Constructor so let's sorry the screen closed so let's open it again we have here the breakfast object and if we're looking at the create method that we're using to actually create the breakfast then we say we the guide is optional and when we don't pass it then we create a new guid so because that's our implementation let's go over here and let's say that the ID is never generated so for that what we want to do is we want to say property and we want the ID and what we want to say is that the value generated never and then antifremacore will know not to generate a value for it we can also say explicitly has key and specify the ID like was suggested before and there are many many configurations via the fluent API with I think it's called the fluid API of Entity framework and there are many things just explore the documentation or when you're stuck about something then just say fluent API whatever it is you're looking for so it looks good let's try creating a migration again so let's say over here the same thing like before and let's see if it worked okay so it didn't work and you might be asking yourself why didn't it work because we have the configuration over here and we're getting the exact same error like before and the answer is that we haven't wired anything together and since thermal core doesn't know to look for this configuration file to do that what we want to do is we want to go back to our Uber breakfast DB context and over here called the on model creating yes and do the following so GitHub copilot is giving you spoilers so I don't know if you like it or not let me know in the comments if I should disable GitHub copilot I'm not sure what I think about it for the tutorials okay so we have the model builder apply configurations from assembly this will actually scan the assembly that we specify over here in our case it's the same assembly that the Uber breakfast DB context is sitting in and it'll look for all those eye and type configuration files or the the types that implement this interface and then it will add those configurations so now let's try calling the creating a migration again let's see if it works great so this time it worked and we can see a few things first of all we can see that we have a new folder that's called migrations and another thing that we can see is that we have here some warnings and the warning tells us that we are overriding the has conversion but we're not overriding the comparer so similar to how when you override the get hash code then it wants you to override the equals as well or vice versa so it's the same thing we're not going to do it in this video but you can search online or ask chat GPT it's pretty simple okay moving on so we have here the migrations folder and over here we have a few files so we have the migration definition we also have here the designer and we also have here the snapshot so starting with the migration then we can see this is the actual migration with the name that we give it and underneath here you can see what happens when the database tries to use this migration that's in the up method and over here we can see that it creates a table with the name of breakfast and now it's very important when you're using anti-framel core to always look at the migration definition you want to make sure that it looks like you want because again we're working with this very high abstraction of using link and using syntax of c-sharp as if we're working simply with objects but behind the scenes we're working with SQL and it's important to understand the conversion between the two worlds so for example this might surprise you that the various Day times are of type text and not something else and the reason for that is because we're using sqlite and this won't necessarily always be text it depends on your database provider so we have over here what we defined so the maximum length is 50 like we wanted for the name for the description it's 150 like we defined we have the ID that is a guid and we have here the two Savory and sweet lists that are converted to strings all right going down then we have here the primary primary key which is the ID and then we have here what happens when you want to downgrade or when you want to go one migration back or remove a migration not sure what the correct terminology is so uh what we do is simply remove the table okay we have that moving on to the designer then we can take a look what we have over here so here we can see some other definitions so this is similar to what we did in the configurations file so we can see here that has maximum length the column type and so on various things that correspond to what we did in the configuration file and you can see that using reflection it's found all the various Day times and added and added it as well okay we have that then the last thing we have over here is the model snapshot and it's similar to what we saw before and it's a snapshot of what the model looks like okay so we have all of this great we have our migration now we should be able to create the database based on this migration so again currently we don't have a database yet this is just a definition of the migration so if this already existed we had this thing then we wouldn't need the design package because the design package is used to generate these definitions but it's it's not needed if you already have it so let's say over here.net empty framework and then we want to say database and what we want to do is we want to create the actual database or to update the database based on the migrations so let's see over here Uber breakfast and then it will actually apply the migrations that we have so running this let's try running it again so so running this then this should create the actual database so let's see if this works okay so it finished running and we can see down here that we have a new file which is our actual database this again because we're using sqlite then this is what was actually created and we can take a look at what we have over here there's an extension for visual studio that's simply called sqlite and add this extension then once you have this extension then you can say over here open database and this will let you choose the database that you want to open and over here this is the path to the file that was created we select this and then we have over here the SQL explorer that allows us to also look at the table definition so everything we defined over here look at the migration history and we can also run queries against it so we can create queries or we can do show table which will give us a visualization of the table so we have all of this let's try running our service and seeing if it actually works so let's say over here.net run and the project we want to run is the Uber breakfast project so this will build and run our application let's let it run and let's try making requests and seeing if it worked if you're familiar with empty framework core then I encourage you to pause the video think what might not work in the implementation that we have also one thing that I wanted to say and I forgot is when you're applying the migrations then you should always look at the output over here make sure that it looks like you expect you don't want to have any surprises down the road okay so we did that and we can take down a notch the disclaimers I think if you're watching this then you got the idea and let's try creating a breakfast let's see if this works let's put here something valid because the name has to be at least three characters so we have over here vegan sunshine this description has to be at least 50 characters so let's just put something here let's say end seems long enough let's try running this and seeing if it works so we can see over here the actual queries that were generated so we can see we have here insert into breakfast blah blah blah based on what we provided over here and in the response we got a valid response which means that it worked as we expect let's take the ID that we just generated and let's try retrieving this breakfast so over here let's paste this ID and let's try retrieving it from the database seeing if the get works so we're running get and we can see that over here it executed the select and we got the breakfast like we expected so it was stored in the database like we wanted to and if we're looking at the actual database so we said we have your visualize uh we said we have here the visualization of the database using this extension so we can see over here that this indeed is the GUI that we talked about with the values that we wanted this is a great way if you're working in visual Studio code to get started and see what things look like using SQL Lite you don't have to install any database all you need to do is choose the sqlite provider okay so we have this let's try deleting the breakfast and seeing if this works as well so delete breakfast let's paste this over here try doing delete and we get no content which sounds good so let's try retrieving the breakfast again let's say get and we get breakfast not found status code 404 that also behaves like we expect now let's move on to the upsert and see if the opposite works so let's copy the ID and let's try doing absurd so I'm pasting the idea over here I'm reminding you that we deleted this breakfast it doesn't exist anymore so creating it for the first time should return 201 created and return the same response like we got over here when we called the create breakfast so we run this and it was created like we expect we got two one created the response looks good okay now let's try updating it and giving it something else that's a vegan Sunshine too and I want you to pause the video and think whether or not this is going to work if you're familiar with entrepreneur core and I'm being very discreet in my hints whether it's going to work or not so we're running this and we get an error so let's look what the heck happened and we can see over here that we have some exception that says that the instance of type of breakfast cannot be tracked because another instance with the same key value for ID is already being tracked when attaching existing entities ensure that only one entity instance with a given key value is attached so let's go back to our breakfast service and understand what's happening over here so let's close all this let's go back to our breakfast service and let's look at what we have in our upsert method so we're looking at the absolute method we have here the breakfast and let's look at what we're doing so a bit of background again this is an introductory video so the dbcontact has a change tracker so what happens is the anti-firmware core tries to be as efficient as possible when it comes to the actions that it does against the database so what we're doing over here let's break it down from this New Perspective then when we're calling find then this returns a tracked object so calling fine won't only run the select query and it will return the breakfast but also antifremacore is Now tracking this object and any change that we do to this object is tracked okay so we have this tracked object and I'm reminding you that we're talking about the flow where the breakfast does exist that is newly created is false and over here we're going to the update method okay so what we have when we're arriving to this point is we're adding another tracked object to our breakfast DB set and now what we have are two tracked objects with the same ID and antifremacore doesn't like the fact that it has two entities tracked with the same ID so there are a few things that we can do the first one is not tracking the breakfast over here so if we aren't tracking over here then it knows that we're updating the breakfast with this ID okay now there are other ways to do this as well but let's look how we can do that so let's start with the naive approach and let's simply add here as no tracking and this will tell antifremacore not to track the object that's returned but find isn't supported in this case because find is intelligent exactly for this to retrieve an object and track any changes to it so we can't use a find anymore instead we can use the first or default or select something else that is supported with asthma tracking so if we do this then this will work but what we have now let's only change this to actually works I'll say B and B dot ID equals the breakfast ID so let's make sure this compiles great so this is the first option but again what we're doing over here is not only are we searching through the database to check if this exists but we're also materializing the breakfast object that we aren't using right so if we're looking over here we have this as newly created or more correctly we're creating this DB breakfast which is based on what we just fetched and we aren't using it so if we're looking for usages of this thing then we aren't using it anywhere so instead let's take a more efficient approach and use any so let's say over here any and let's say any where the ID this is the breakfast ID and this will return true if it does exist which means it's not newly created so let's say not to invert this logic and now this should work like we expect and this is the reason why it's very important when you're working with antifremacore to understand what's actually happening behind the scenes whether or not you're doing one trip to the database like we're doing in some places like over here for example or whether you're doing two trips to the database like over here where we're checking if it exists and then we're doing another trip to the database to actually update or create a new resource and same thing goes for the other ones so I really encourage you to always make sure that the queries look like you expect to have tests in place this is something that I can't emphasize enough so great we did that now let's try running the application again and seeing if it works so let's say run and let's go over here let's go to the upsert and now let's try creating again a new one with a new idea that doesn't exist let's wait for it to build and run and let's try making the request great so making the first request then let's see if this works then yes we get to one created and the ID here is indeed the new ID that we just invented it's vegan Sunshine too so let's call it vegan Sunshine three for the updated value and we get no content which sounds good because that's what we want so let's try retrieving this breakfast now and it's using the new ID then we can see that it is retrieved and if we go to our amazing sqlite viewer then we can see that indeed it's sitting over here with the one two three with the new value and everything that we specified okay the last thing I want to do is I want to attach the debugger put a breakpoint and to investigate a bit the various properties so you're more familiar with it so let's go over here to the run debug and select.net core attach and now when we click then we need to choose which process we want to attach it to in our case we want to attach it to the Uber breakfast process that's running this will attach the debugger to what we're running in the terminal so now that we have that let's make the upsert request again so let's say upsert and let's change this to four instead of three let's make the request and I forgot to put a breakpoint so nothing happened let's go to the breakfast service and let's put a breakpoint in the beginning of this method and let's make the request again let's change this to five and let's hope this will be the last increment so we hit the breakpoint I want us to look at the DB context so we have here our DB context and like we said there are a few things here that we care about so we have the change tracker that we're already familiar with we have here the breakfast DB set that we're also already familiar with all right let's step over the is newly created line and let's continue we have that we know that it's not newly created and over here we want to update the breakfast and now we haven't called save changes yet let's look at the change tracker okay so opening the change tracker then we have over here the non-public members that internally has the state manager that internally has the entries that internally has here the entry that we just modified and if we look at the entries State entity State sorry then we can see over here that it's modified and anti-frame core has been tracking this object it knows that it's modified and now it knows that it needs to update it so I think that's it for now we'll dive into deeper things in the upcoming videos on the Uber dinner project so if you're not subscribed yet then make sure to smash that subscribe button so that's it for this video I really hope you enjoyed it and you learned something new let me know in the comments if you would have done anything differently also if you have ways to make what we wrote Today faster less trips to the database or in general making the speed of what we wrote Today faster then make sure to put your tips below because people watching this video are going to be people who are starting out with anti-firmware core and I'm sure they'll appreciate any tips tricks and anything else also if it's something new that I didn't know then of course I'd love to know about it and also present it in a future video so that's it and I'll see you in the next one
Info
Channel: Amichai Mantinband
Views: 42,531
Rating: undefined out of 5
Keywords: entity framework core, entity framework, asp.net core web api, entity framework core tutorial, ef core, dotnet, .net, c#, ef core c#, ef core code first
Id: v19arLqQkP8
Channel Id: undefined
Length: 40min 56sec (2456 seconds)
Published: Wed Dec 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.