Creating Data Storage with the Entity Framework Core
A free video tutorial from Packt Publishing
Tech Knowledge in Motion
3.9 instructor rating • 1423 courses • 345,245 students
In this video, we will create a data storage with the Entity Framework core to store data into the database.
Learn more from the full courseRESTful Web services with ASP.NET Core
Leveraging ASP.NET Core and Visual Studio 17
03:33:54 of on-demand video • Updated August 2017
- Explore basic REST principles
- Build an ASP .NET core project using Visual Studio 2017
- Work with different environments such as development and production
- Implement a service interface to read, manipulate, and delete data
- Extend API functionality with more features
- Deploy the WebAPI
English [Auto] Well come to the third section building the first endpoint with esprit de corps of the Course rest for Web services with Espino knit Corps in section 3 we will create our first endpoint with Espino and core. First we will explore and frame of course and create the first data storage with it. Then we would create a first controller as a first endpoint and create routing on it then we will see how we can call and receive data from this controller. We will be able to add data to the database to manipulate data and delete data in the end. In this video I want to talk about how do you create a data storage with entity from a car in this video or we will look at how to install and use entity from a core in our application. We will create a date for a presentation object of the data we will send over the wire and we recreate the repository within the interface for this data representation we can use with our database. Ok so lets start before we start using a SQL server with a different recall you'd have to install askers of a first. He can go to this your out here on Dachs Microsoft app come to get information how to install askers server. You can also do all of the skills of a management studio or ask your server 2014 Express so you can go to this link right here and just click the download button. Sure click the download button you can scroll down a bit and then you will see Asgill EX-PRES with tools. And then you can choose your appropriate Ex-Sea and just download and install it. I already did that so I can connect to my local askers of instance which is running here that you can go to the studio and you see a packed web app which we've been working on so far. The first thing we need to do when we want to work with as a server and end to a different record is to install and different a car so we can do a right click and save manage to get packages here and there and we will search for Microsoft and a different car and then you will see a package which is called Microsoft. And in of were caught we couldn't solve it. But what we need is we need and do from a court and we have to access it over the server. So we will use a different core and we need to provide for escort service so what we can do here is search for the ask else. And if we install this you can see here that on the dependencies every dependency see which is needed is also installed. So the different core comes with this ask serve a good package and this makes it a lot easier for us. So we really saw that accepted okay we were ready. Now you can check in the dependency section on Nouel good. You can see that and do different core escrow servers install. Okay. Now we can start using it. Okay so let's create a representation of our database. So what we can do in our project we can do a right click and say add new folder and I would call this entities in these entities folder we can create a new class learn to raise you and a core class and we call it PAC d contex and this class we derive from D-B contexts DCV context is stored in the entity framework core namespace. So we will do that and then we will do a constructor here and pass in the D-B contacts Chinse off this package DB context Carlat options and call the base constructor with these functions. OK so the next thing we need to do is we need a configuration for our database to store a connection string which tells us how to connect her database and how our database should be called for this we can go into the app settings start Jason and we can just give the configuration which has a section connection strings and a connection string which is called default connection and it points to server localhost as you Alex for us database should be called PAC database. And we said the trusted connection teacher. So if we save this now we can consume in our configuration of our location. This default connection string and therefore we can access or start of the C-s file. First of all we can get rid of this configuration here and we can get rid of this one here. And instead we can say Services dock at the service dock at D-B context which is the packed D-B contexts and we have to add the appropriate using as entities and in the constructor of the active context we will pass an options object Chinse and we say options to use as a server and to use this as because of what we pass in from our configuration. You get connection string. And then we point to the connection string we just call the phone connection and we pass it in as a string here. OK so what we are doing is we're calling at D-B context with our pack DB context and passing up Chin's parameter. And on this parameter we call a use Eskdale server configuration get connection string and then use the default connection and diskette connection string is a method from Espino net core which is automatically accessing the connection string section in your app settings. Jason for so now we're pointing to the correct database. Now let's build a model representation for something to send over the wire to create an object which we will send over the wire. We can just use a normal class and that will go into our entity folder. And to add in new class which is called Custom so to do you created a new class and we will give this class customer a few properties. First of all it's called an ID with a type Kuwait. Dennis Turing first name a string last name and an integer. H. So this is the object which we will store into our database and then we can go into our PAC D-B context we can. And this one as a property and we can just D-B said and this should be our customer and we call them customer get set. OK. So right now the context is aware of that. We want to store customers. OK. The next thing we need to do is to create a repository for that so that we can interact with a database using this pact to be context to add a repository to our project. We need a new folder called repositories. We do it right click and add in your folder and call the folder repositories. You go to right click and see at class and call the customer repository. There you go. And if we now take a look at the startup file scroll down a bit and here we can see that on the services collection we added to DB context. And we said that we can use this services collection to use the dependency injection pattern from Microsoft Espina on that core. So right now we added the DB context in the service collection so we can use it in a repository and injected into construct of our customer repository. So we do a constructor here that we can now say okay let's inject the packed D-B context here and call it context adding the appropriate musings and store that in a private member pack TV contacts on that score. Context and here we can say contacts he calls contacts. There you go. So now we are just zooming in the back TV context from the services collection and this is working because we added that back to the context in the start up to service collection. And now we can implement the whole repository that prepared something here. So we see that we have all the methods we need to make the credit variations but we see red lines there. What are they talking about. They're telling us inaccessible due to it's protection level. OK. Why is that. Because of the packed context we have to make or D.B said public. We say that the red line should be gone. And there you go. OK. So on this proposal don't we have it get all method which is just returning and I courrier Bill of all customers were asking the context for the property customers to get single method is taken in ID and is just searching on the context customer's property for a customer with the appropriate ID the method add adds something to the context of the customer's property. They delete method takes in ID is searching for the customer to remove and then calls contact stopped customers don't remove update is just calling update on the customers off the context and safe called Safe changes so that when we modifying the contacts we can do this in multiple steps. But in the end we have to call save and this is firing all the changes to our database. Okay. So thats our repository for that but we need an interface for that repository so that we can use dependency injection in appropriate way. Microsoft Research to discover does in that case and we can do a right click on the repository and can say quick actions and refactor things that we get the possibility to choose extract interface. We can't get a name and you find them we can choose all the methods which should be X-post we say OK and Ragg now we have an interface. Cool. Now lets check if for customer Depository is implementing that interface. It does everything went automatically. So now we can go terrorist file and right now we are doing at D-B contacts only. But we want to add our repository as well so we can say Services dot at scope's because for every HTP request we want exactly one instance of this repository that we can say I custom repository and customer positon or just add in the appropriate using is OK. There you go. The red lines are gone and this is how you can add custom repositories to the service collection from a pure net core to use it with to build a dependency injection. So one last piece is missing because you want to create a database. We can do that with Visual Studio. The first thing we need to do is we need to modify our base path instead of directory get current directory. We will use the environment content route path. If he did that he is now searching for the app settings start Jason connection string in the right place that we need the migrations file and we can add that by going to the package manager console and type and dash migration and give it a name. Let's give it the name first migration immigration file is a file which tells a different bug how to create the database and you can see the background of first migration class was created. And if you now type update the ash database and hit enter your database will be automatically created. I already did that for the course so when your head you server Explorer now you can see under the data connections when you're adding your local house server that Dipak database was created in this case. If you open that up you can see on the tables our customer table right click on that and you can say show table data and you see that the table was empty for now to add something in there. This will be the content of the next video. So this video we saw how we can install and use any of different MacColl we used nuclear to insulate. And we could access it via the D-B context the different would call namespace gave to us. We created the data representation for objects which our customers in this case and we added them on that D-B context and then around that we could build a repository where we can work with these customers under the context and we added the repository to the dependency injection system which comes from Espina in that car. And then we saw how we can create the database with Visual Studio and we saw how we can see the day that were just stored into the customers table in the next release or we will see how we can create the first endpoint with as Peter net core.