Entity Framework Core + Blazor - Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're a c-sharp developer and you've not used it yet you've probably heard of entity framework but what is it and what can we use it for let's figure it out in this video before we get into the video please hit that like button and if you want some more.net content then please subscribe to the channel so what is the entity framework well in a nutshell the entity framework is an object relational mapping framework or orm for short these frameworks allow you to work with data using objects rather than straight tables directly in the database so in this case we'd be using classes it means you don't have to touch the underlying tables and columns in a database and provides a higher level of abstraction therefore making things just a little bit safer and easier to manage you can use your classes to create update delete insert new records as you need to and you can query records using the same query conditions you would use like a where clause for example if you were using something like t sql so instead of writing a straight sql query you will be writing a link query using entity framework and the objects that you've abstracted above your table instead of creating a database connection directly to the database in entity framework you create what's called a database context a context is an abstraction over that underlying database connection that gives you access to the tables and columns of your database in the form of classes and objects you can also use entity framework to alter the schema of your database using a concept called migrations which we'll go into a bit later on in the video so for example if you removed a property from a class that could remove the underlying column from the database so for this video we're going to take what's called a code first approach to entity framework now what that means is that we are essentially creating the database from entity framework in c sharp rather than creating the database schema from sql itself the alternative to this would be a database first approach which is where you have an existing database with an existing schema and you need to create an entity framework context for that database and then you would essentially scaffold that entity framework context on top of that existing database schema we're not going to do that we're going to start from the code and create the schema from c-sharp we'll cover creating a new database querying and updating data in that database and managing the schema with migrations so we're going to use blazer for this but you could use any net application to create an entity framework context you could use an asp.net application you could use a console app but for this video i want to use blazer because we've been doing a lot of blazer recently and it seems like a natural progression so we've got the sample application here i'm in the weather forecast service at the moment so the first thing we want to do is create is install the nuget packages for entity framework core so i'm going to head over to manage nuget packages for solution and in browse if we just search for entity framework and then press enter we should see microsoft.entity framework core so i'm going to take this i'm not going to install the pre-release because i don't want any surprises so i'm going to use the latest stable put this on my project there we go and click install and that will create i will install the entity framework core packages so we'll click ok for that i should install everything i think it's already done and then we also want to install the provider for the database type that we're using so we're going to use microsoft sql server so we need to install the sql server provider but you can use entity framework with other kinds of databases as well including mysql but obviously for us we're just going to use sql so i'm looking for microsoft entity framework core dot sql server and i want to select the latest stable and install it's going to install that and there we go okay so now that we've added entity framework core and the sql provider the first thing we need to do is create a class for our db context so i'm going to head over to my data folder i'm going to right click it and click add and i'm going to create a new class and i'm going to call my db context demo db so demo db context okay so there's our class and i want to inherit from db context so this is an entity framework class and you can see it doesn't recognize the namespace so if i click show potential fixes you can see it's suggesting microsoft.entity framework core so if i use that it works okay then i want to add a constructor so public demo db context let's make this a bit bigger there we go and this will take a set of db context options of type demo db context i'll call this options and we want to pass this to the base class because db context is what we're inheriting from so we'll pass those through and there we go so we've got our db contacts set up what we'll do in a second once we've got our database configured is we will create a bunch of classes which will represent our tables but first of all i want to get a connection to the database setup i want to get i want to get the contacts set up and available as a service in our blazer application i'm going to show you how to do that so in order to create the db context as a service in blazer we use what's called a db context factory and if we create a service for that in the program.cs it means that we can use dependency injection to inject a db context factory into any service that we use so for example in blazer we've got weather forecast service which is just a class that we use from the front end we could inject a db context factory into one of these services and then that would allow us to get the context we want the database essentially and we can then get any of the tables query things insert things all that good stuff so back in program.cs i'll show you what i mean we're going to go to builder dot services dot add db contacts factory and then the type of context factory will be demo db context so that's added a db context factory to the services of our applications which means we can use it as part of dependency injection elsewhere in the app now remember that our db context at the moment we've created a db context factory with just a plain demo db context but remember our constructor takes db context options so we need to pass those in so i'm going to pass in dem db context options builder as a type i'll just hover over that and it just needs entity framework core and i'm going to wrap that in parentheses because we're going to do a anonymous function over there so we'll call this options and we'll close those parentheses and then we use an arrow function and say options dot use sql server i'm just going to put this on to a new line for clarity or readability and then this is where we add a connection string so we don't have a connection string yet we need to set one up so i've just opened sql management studio and i'm going to go to my databases and i'm going to say create a new database so i just usually like to create a blank database to start with and i'm just going to say demo database is the name click ok and we should then see demo databases there it won't have any tables it's a fresh database then if we head back over to visual studio one of the things you can do to easily get the connection string is go to server explorer and then make a connection to that database so if we say add connection and then for me i'm using my local sql server so i'm just going to do dot slash and then select the database so i want a demo database click ok that will show me the database in data connections if i double if i right click it and click properties on the right hand side here you can see there is a connection string so i'm using the windows authentication so that's just going to use the integrated security set to true now we need to put this somewhere in our application so that we can grab that string and use it as the connection string when setting up the context so in blazer there's an app settings.json file it might be a bit different in some other applications like asp.net mvc sometimes if it's not.net core might be web.config for example but there's usually some kind of configuration file for other applications like console apps you can still use configuration files but if you want to you could store it in a text file you could hard code it if you wanted to it really is up to you but for blazer i would say the best practice is to use your app settings.js file now this is a json document so you can format it in json format obviously and for this i'm going to use connection strings which is suggested already by visual studio and connection strings is an object so we'll just create that and then you give the name of your connection string so i'm going to call this connection string default that's usually standard practice and then you would paste in your connection string like so this now means that you can access this configuration from elsewhere in the application and get that string so that you've always got your connection string somewhere handy so i'm going to create a variable at the top of program.cs called connection string and it's going to be equal to builder.configuration.getconnectionstring so this will go to the default connection string object and then you can get it by its name so you can say default now one of the things i do like to do with these is do a null check so i'll put a double question mark and then i'll say if it's null which it shouldn't be but just in case i'll say throw new null reference exception and say no connection string in config exclamation mark so at least we know that if this connection string was null in the json or if it didn't exist then we could throw an exception so now we can go back to our adddb contact context factory function and we can add connection string and there we go so we've added our db context factory which means because we've added that into the services in program.cs when the application starts it will create that service and it will allow you to use dependency injection to inject that factory into other classes and therefore use the factory to get the context which is your database connection so at the moment we've set up the dependency injection and everything like that in order to get the factory which gives us access to the context context which gives us the tables and columns that we want but the moment we have a blank database how do we set up the database with tables okay so let's go back to the demodb context and here we can define our db sets which are essentially the tables in the database but in order to define those db sets they need to have some kind of model to model the table so what we can do is create a few classes which model the tables we want to create so if i create a new folder within data so i'm going to go create new folder and i'm going to say that's called models and we're going to create a new class and we're going to use this as a table so let's say we wanted a table called customer so i'll create that and then we're just going to set this up with properties as needed so we've got a string called name we can get it and we can set it so we've got a property for that we also want to be able to identify this customer uniquely so it's good practice for these kinds of model tables to have a primary key and you can do this by creating your property so i'm going to say create an id property which is an integer and by default that will use the incrementing identity column so the first customer that's created for example would automatically get an id of one and then it will go up two three four as as you go but at the moment entity framework doesn't know that that's your primary key so we use data annotations which are attributes to tell entity framework that this property is a key so if i use square brackets and type key at the moment that doesn't get recognized but if i hover over it show potential fixes and we just need to add a using statement for data annotations now we've done that when we eventually create the database table from c sharp entity framework will see that key against the id property and it will say okay this is a primary key it's an integer so i'm going to create one that gets incremented one by one as records get added so we've got an id we've got a name we can create a yeah let's go age that's been suggested to us by visual studio which is great and we could also say public yepdatetime createddate as well we'll do that as well so that's our customer object and this is basically a table that we're going to create in the database we've created this inside a folder called models and it's going to be now added to our context so let's go back to our demo db context and i'm going to add a new member called well it's a public db set and it has a type of and it's going to be customer so then we'll call that customers and we'll say get set so then we'll just add a reference to the namespace data.models and there we go so we've told the context that we've got i've put that in the wrong place i'll put that inside the constructor and put that inside the class there we go i'm going to give that a capital c as well so we've got a db set now of type customer called customers so the idea here is that we are essentially scoping out what's going to be the schema of the database but how do we actually get that schema created on the database side well that's where migrations come in so migrations are essentially a change log so it's a way of understanding what you've changed in the code and translating that through to the database so what you essentially do is you whenever you make a change to your schema in the code you add what's called a migration you use a console command called add migration and entity framework will look at all the changes a little bit like git when you do a commit and it looks at all the changes that you've done and applies those changes to the branch we're getting entity framework to look at the changes you've made so in this case the change we made is we've added a db set of type customer to our context which means that we need to add a new table to the database so if you say add migration then you also have to give that migration a name typically the best practice is to again similar to get like a commit message write the change that you've made so for me i would say that this migration is added customers table once you've done that it then updates a set of migration classes and then you tell entity framework to then update the database and the database has a track of which migrations it's already applied so if it hasn't applied that migration it will do so and it will update the schema so let's go through this step by step so in order to add your migration you need to be able to type on the command line now i like to use the nuget package console or the package manager console so if i go over to tools nuget package manager and package manager console then you can access your command line from this package manager console at the bottom and add the migration simply type add dash migration and then you need to pass a string which is the title of the migration or what you did so i'm just going to say added underscore uh customer table click press enter and we've got an error so what's the error here okay the first thing that i've done wrong is i forgot to add another nuget package so we actually need to add another package on top of the two that we've already added in order to manage the database from the command line and this is the entity framework tools nice and simple if you head over to nuget package manager and search for entity framework again we should find the tools here there we go so framework core.tools we'll add that again i don't want the pre-release because i don't want any nasty surprises click install finish off that installation and then hopefully if i go back to my package manager console if i try the same command again i should be able to add that migration so it builds the project first and it looks like it has created the migration and yes so the way you can tell obviously besides not having any error messages in the console is that you'll have a migrations folder and you will see two classes appear a snapshot and the migration itself so let's take a look at this then so the snapshot is essentially a snapshot of your prospective database of the database to be created and you can see that there's some code here which is updating various columns and constraints and all that sql stuff that you would probably do directly in sql management studio if you were doing this directly against the database but the key thing we are interested in is this migration class here so typically visual studio will create this or entity framework will create this class with the timestamp followed by the title that you passed in when you created the migration and what you can see here if you look through is essentially an abstraction of the sql commands that will be run against the database so this up essentially is when you're applying the changes and down is reversing the changes so if you had to reverse a migration it would run this whereas what we're going to be doing by default is applying the migration and so up will run and you can see here that we're creating a table its name is customers it's adding a column of id which is of type integer and it has the sql server identity applied to it so it will increment one two three four and that will be the primary key we've got the name column which is an nvar car max we've got age which will be an integer and created date which will be a date time and then you can see yet the constraints there is that primary key against id so you know just by changing the code and adding a class of customer and then telling the db context that customer is a db set therefore it's a table we've been able to generate all this sql code which is going to apply everything to the database so from a developer perspective uh you can stay in c sharp for the for the most part and you can just say okay i'm going to check the migration okay that looks good that's what i expect to happen i want a new table all those columns look good so that's great so we've got a map for what's going to get created on the database side now we want to apply it so we'll head back over to the console so the package manager console and you can simply type update dash database press enter and it should start running those sql statements so there we go and there's no errors so that looks good now if we go back over to the database just to take a look at this and i'll click refresh head over to tables and you can see here we now have two tables so by default we always have this dbo.ef migrations history we'll take a look at that in a second the one we're concerned with is the customers table which has now been created so if i select the top 1000 rows obviously there won't be any data but you can see we have the columns we expected to have we've got id name age and created date so just by adding a class and then adding that class as a db set in the context within c-sharp we've been able to create a table in our database now i mentioned that ef migrations history is a standard table whenever you're managing databases with entity framework core if we take a look at this you can use this to see from the database side which migrations have been applied and this makes sense because we can see this is the migration that we created in visual studio and that's been applied to the database and this is a really useful tool if you just want to see if the database is in sync with your code and what migrations you're expecting to be applied when you run the update database command so let's also create another table then and we could also start to create relations between the tables remember sql is a relational database so you're going to have foreign keys which relate to other tables so what we could do is create an orders table and have each orders table relate back to a specific customer with a foreign key relationship so again we'll repeat the process we'll go to models we'll say add new item and we'll create an item called order so it's a class called order and we'll just do again we're going to create an primary key of id there we go we've got that there we're going to add the attribute of key and add a reference to data annotations as we did before and then we'll do public string we'll do a item description we'll do a an integer of quantity so we'd say how many have been ordered and then this is a crucial bit we want to add a relationship to a customer that's placed the order so we can say public customer customer and this should apply a relationship between the customers table and the orders table so let's save that we'll go over to our demo db context we'll add a new db set of type order and we'll call it orders and then we'll add a new migration and we'll call it added orders table press enter it will build the project and we should have a new migration which i can see here so if we look in that migration we're creating a new table we've also got a constraint which has the primary key but it also has a foreign key relationship to customers on id so it should show a customer id in that table so let's test this we'll go back to the package manager console we'll type update database and there we go and let's just go back to sql refresh this database go to orders which is the new table we've created and you can see here we have a customer id we didn't tell it to add a column called customer id but we said that there is a customer reference on the table so entity framework automatically told sql translated that in the migration to a foreign key relationship to the id on the customer table so this is really powerful it allows you to create these relations really simply through code so we've been through migrations and how to create the database will create the schema from c-sharp using entity framework now let's take a look at how we can actually insert data uh interact with the database through the front-end of our blazer application so i'm going to create a page which we can use to add a new customer so we can add the name the age all those properties that we added to the model in a form submit the form and have that data inserted into the database so the first thing we want to do is create a service which can act as the means of retrieving data from our context or adding data to the database through the context so it's kind of like a middle layer between the front end and the database so i'm going to go over to our our solution explorer i'm going to create a folder on the project called services this is what you tend to do with blazer whenever you want to create a data layer usually it's best to create a service which has the responsibility of speaking back and forth between the database and the front end can then inject that service or use that service on the front end to then send data from the ui to the back end so we're going to add a new class so we're going to call this class customer service and then click add and then we're going to start adding some properties so the first thing we want to do is we want to make sure that we've got a db context factory available on this service anytime so that whenever we're using the service we can use that factory to get a context so i'm going to create a private member and it's going to be in i db context factory and if we then hover over that we'll get the entity framework core reference and i'm going to put an underscore for a private field and i'll call this db context factory then i'll put in a constructor for customer service and on the constructor i'm going to inject an idb context factory because we created the service in program.cs therefore this is where we're going to use our dependency injection so we're going to inject this into this service i'll call this db context factory without an underscore and then in the body of this we're going to say that our private db context factory is set to the one we've injected now one thing we forgot to put on here is the type so it's an idb context factory of type t of t sorry so we need to specify the type of context which will be demo db context there we go add that reference and there we go same up here demo db context so we have our private db contacts factory and we have that initialized through dependency injection in the constructor of this customer service class so now i can add some crud actions so i could create some functions which take care of updating of retrieving of querying all that sort of stuff so what we're concerned with for this is to be able to insert data so i'm going to create a method called add customer and that's going to take a customer parameter so we'll add a reference to our models so customer customer and this is where we then get a context using the factory so we'll use a using statement because it's using eye disposable and we'll say we're making a new context which is the result of db context factory which is this up here dot create db context and that will use the db context builder options that are part of that factory to create a context to the database that's connected to so now we've got access to our database through this context variable so here if i type context and then do a dot you'll see all the different methods that we could access and as you can see one of the available items is customers because there's a table called customers or a db set that we created called customers there's also an orders object as well so by adding or querying these objects we are in effect adding to or querying from the database so we want to add a customer to the database so we would say context dot customers dot add and then the customer that we pass in and there we go simple as that so that's a very simple method which means that from the front end if we were to inject this service into a page we could then call this services add customer method passing in a model and the service will take care of adding it to the database how do we inject this service into the front end well we did go over this in a previous video around blazer specifically but i'll just go over it very briefly so because we've created a service here we want to also register this for dependency injection so i'm going to go builder back in program.cs builder.services you can see there was a singleton added for the weather forecast service so you can choose to say add singleton if you like but i'm going to add a transient because i want to be able to have multiple instances of this if needed so add transient and it will be customer service and we'll just get a reference to our services there we go so this service should now be available for dependency injection in a front-end page so let's create a page um or better yet if we go to our index page we could put it on the front page so what i'm going to do is at the very top i'm going to say at inject just want to inject our service and i think it will also require a using statement yes it will there we go so we inject an instance of customer service and now we can use that service in the front end so let's just create a brief uh form so that we can create this data from the ui and insert it into the database so let's take a look at the model because essentially we're building a customer so we don't need to worry about an id but we do need to create a name an age and the created date we also need to handle from the service because we need to be able to use the actual date or time that it was created so really for us the only things we want to be able to create from the front end are the name and the age so we need an instance of a model in this front-end page so we need a customer model essentially so down in the code section i'll create a code section here and i'll say for customer equals new customer i just need to put in blazer server demo [Music] dot data dot models cool so we've got our customer instance so in our edit form we can create an input box i'm going to use the blazer html helper and we'll give this an id we'll call it name input and we also want to bind this to a property on the model so to do this in blazer you can say bind dash value then you would set that to customer dot name so we've created a binding between this input and the customer property on the model we also need to create a label for this so just above it i'll create a label and do a for attribute and say that it's for the id of name input so that it links to that input text and then we'll just put name and then i'm just going to copy this and do the same thing for age so for this one we want to bind it to customer dot age now the issue that i've got here is that age is actually an integer so i need to change this to an input number so that i can pass in an integer and it will enforce on the front end that i must use numbers and not text now i need to be able to handle submission of this form and i haven't actually created a button yet so i'm just going to put i've typed submit and call that create i think i'll also give it a class of button just button primary i'll be fine okay cool and then we need to be able to handle this so let's create a method inside our code block called handle submit and this will take an instance of edit context which automatically comes from an edit form in blazer now one of the things we need to do with the form is actually give it a model so we need to say at model equals and then the model that we have created so that when we pass this through for handle submit it will have that model assigned to it and that model that's sent through as part of the edit context will be an object so we can then cast that back to its original object which will be customer so i'm going to create a variable called new customer and i'm going to be casting this so it's going to be customer and then it's going to be the edit context dot model so whatever the model object is we're going to cast it back to a customer which will give us access to the items that were bound so we'll already have the name and the age the id we won't need to worry about but we still need to update that date so we can say new customer dot created date equals date time dot now and that will take care of updating the date to the point that it was submitted then we can use our service to add the object to the database so we can access our customer service and then say add customer passing in new customer and there we go okay so looking back at what we've done then we've created our form there was one thing i just needed to change which i made a mistake on which was that originally this model attribute had the at symbol before it we don't need to do that we just need to specify the model using just the word model and then obviously customer is our model as we've created a customer variable here so now we've got our form we now have to actually link that handle submit method to the submit event on this form itself so for blazer we can say on an edit form on valid submit equals and then the method or function we want to use so we can say handle submit and that will then link the submission of this form to this method and it will receive the edit context from the edit form from that as we saw we can then pass the customer from the model and then handle the actual contact or interaction with the database via entity framework so let's debug this let's run blazer and i'll put a breakpoint in this handle submit method and here's the running app now the color scheme is a bit different because you may have seen in a previous video we changed some of the css and the styling on the application so it's just the same project which is why you're seeing that color so here's our form we just put it onto the main index page so we've inserted it below the template that's on the first page you see on a blazer application so you can see that we have our name text box and our age text box and then the create being the button we would use for submit so i'm just going to put my name in there and my age which is 34. gonna be very open click create and you can see straight away we've hit our handle submit method so we'll step through we're going to create a new customer passing from the model so let's take a look at that we've got age 34 created date at the moment is still incorrect id is zero because there's no records in the database and the name is nick so let's go to our add customer method i'm gonna step inside that we're now inside our customer service so we're going to create that context from the factory so that that context seems to have been created that's our connection to the database that's all good and then we're going to run the customers.add and there we go now will we see this in the database now i'm going to say no i wouldn't expect to see this in the database and i'm going to show you why so i've just opened up the demo database i've gone to the customers table and i've done a select uh select all select star on that table and as we can see there are no records so there's a reason for this there is an extra step we need to take an entity framework to actually commit the data that we're inserting so if i go back over to our blazer demo and take a look at the customer service in here this is where we're doing the customers.add but at this point we're simply adding the customer object to a db set so what we've added is still in memory it's not persisted into the database in order to persist what we've done or save the changes we need to call a method on context called save changes and what this will do is it will take the item that you've added in memory and it will actually execute the persistence to the database so let's run this again so i'm going to once again say nick 34 32 wishful thinking click create we'll go back to our method go into add customer create the context add the customer to the customer's db set save changes and that seems to have worked you can see here in the output you've actually got a sql query which has been written out which is the actual query that entity framework will have executed against the database so if we go into the database and run the select star you can see now we have a record and it has an id of one which would expect the name nick 34 and the created date is the date that what's the current date and time so without that save changes we're not actually committing the data and this is a really good example of simply how we can insert something into the database but we could use this similar method for other kinds of actions on the database we could update a customer we could select a customer so for example i could create another method inside this service called um get customer by name passing in a string called name and what this means is i can say obviously get the context again so i want the db context factory create db context so that gives us our connection to the database and you can see visual studio is already giving us an example of how to get a customer by name so i'm actually just going to type this out properly so the customer we want is in context dot customers so in the customers table and then we can say single or default which means get a single record or the default which would be null if there were no records returned and then we're going to use a lambda expression to say what the customer should be what the actual condition is so here we're executing a linq query against the database so we're saying for x where x dot name is equal to the name that was passed in so it will find the first record or null if it doesn't exist where the name is whatever i pass in so we can just uh test this by i'm going to debug up to this point and then when we get to this point i'm just going to try to run this query in memory or in the immediate window in visual studio so we can get an idea of how it would run so the app's running i'm just going to put any old data in here and then we're going to hit this handle submit and here we are just about to save the changes but i just want to try this method out while we're running so in immediate in the bottom right see if you can see that might be quite small writing but i'm going to call that method which was get customer by name and then i'm going to type the string which is nick i know there's a record in there for nick so we'll do that and obviously i've made a big mistake there and that i've made the method void i actually want to return a customer so i'll say public customer return customer so somebody could call this and get a customer back obviously there's a possible null reference here because single or default could return null if there isn't a record and so you'd have to do a null check on whichever method is calling get customer by name so it's just get up to that point so i'm going to call get customer by name nick that should just give us a customer and there you go if you can see that small writing there in the bottom right we've got a customer age 34 created date id is one and the name is nick and what we get back if we pass in an incorrect name so if we look for uh john we know there isn't a record in there with the name john and it returns null so we can see the query that was ran and the result being null and so you'd be able to say if the result is null then there are no customers for that record we can also do updates so if we were to make another method um public uh we'll do void again so update customer by we'll say by name so say for example you wanted to get the customer record by name and then update something on it so if i wanted to change my age for example then i could call the above method that i just made so i could say for customer equals get customer by name passing in the name remember i need to do a null check on it so if customer equals null i could uh throw an exception customer does not exist cannot update you know something like that but if we do have a customer then we could say customer dot age equals and then we could also pass an age to this as well so i could say int age and then set that to age so we've essentially pulled the customer record into memory from the database and then we're editing it in memory and we need to persist it again so what we'd need to do is then get a context so then we'd say var context equals dbcontextfactory.createdb context i'm going to wrap that in a using statement and then i can say context dot update and then pass in a customer so what this will do is it will find the record with the appropriate id so we'll use the primary key and it will overwrite that record with the new record that i've created in memory and then obviously i want to save the changes there we go so just to go back over what we did passed in a name to find the record and a new age we then got the customer by name and if it wasn't null then we change its age then we get a context using our db context factory we use the update method to update the customer as per the latest version of that customer and then we save the changes on the context so again i'm going to run the blazer demo so we're in the customer service and then if i call update customer by name passing in nick and my new age which is going to be 21 and then press enter and there we go so let's take a look at the database 21. so you can see we've successfully updated an existing record and it was as simple really as grabbing the appropriate record out of the database using a link query so we use get customer by name to get our customer record we then just changed the properties of that customer in memory and then recommitted it to the database with an update and save changes there's also a remove method so if you wanted to delete a record you could say remove and then add in the customer that you want to remove so that would actually delete that customer and so you can see here how you would simply use the various link queries to get hold of a specific record and then make changes to that record as you need to so just to sum up we're getting the records from an in-memory data set and then we're changing them or inserting new records into that data set and then committing the changes to the database from entity framework now there's a lot of different things you can do with entity framework anything that you would do normally within sql for the most part if it's if it's to do with manipulating data in tables you can do it in c-sharp as well via entity framework obviously if i was to create an order here then what i could also do is then use get customer by name to get the appropriate customer and set the customer value of that order to that customer and it would automatically link them together as relations in the database and so you can see how it's quite simple as a higher level of abstraction to create those relations in c-sharp rather than directly writing code in sql the benefit of it is that it allows you to be a bit more defensive so it's typically quite dangerous to make a developer sit in sql management studio and write raw queries because if they write the wrong drop statement or delete statement they could be deleting production data the thing with entity framework and wrapping the sequel in a c-sharp bubble is that you can defend against things like that by saying uh you know are you sure or just checking against you could add in other defensive business logic to make sure that that record should actually be deleted which is often quite important so you can see that gives you a lot more versatility and a lot more safety in terms of how you interact with and manipulate your data in sql i really hope this has been useful as an introduction to entity framework core probably going to do some more videos and entity framework in more detail but if you have any ideas around entity framework or there's more questions that you have then feel free to leave a comment uh below and don't forget to hit the like and subscribe buttons it really does help the channel and i would really love to make some more.net content so please do subscribe until next time keep coding keep enjoying.net and i will see you soon for more c-sharp goodness
Info
Channel: Nick Proud
Views: 10,302
Rating: undefined out of 5
Keywords: .net core, asp.net core, blazor, blazor app tutorial, blazor c#, blazor crud, blazor database, blazor ef core, blazor entity framework, blazor entity framework code first, blazor entity framework core, blazor server, blazor server entity framework, blazor tutorial, c# database, c# tutorial, c# tutorial for beginners, code first approach in entity framework, database, ef core, ef core 6, ef core tutorial, entity framework, entity framework c#, sql
Id: yrzNkGeAdno
Channel Id: undefined
Length: 52min 46sec (3166 seconds)
Published: Thu Sep 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.