In this video, Nate demonstrates how to use lightweight data models in conjunction with the Entity Framework in-memory database provider to seed the database with test data.
- [Narrator] Since we're using an in memory database for development, any data we save will be lost when the API restarts. This means that the database will be empty when the application starts up. To make it easy to consistently test the API with the same data, we can seed the database with test data on startup. We'll do this in the startup class. Earlier we added the Db context to configure services. Now in the configure method, right before the application starts up, we can pull that context back out and add some data to it.
We only want to do this in development. (sound of typing on keyboard) So, we'll check the hosting environment to make sure we are in development, and if so, we'll pull the context out of the application services container, which is what's configured in the configure services method. We'll say get required service of type Hotel API Context. And then we'll call a new method called Add Test Data and pass in that context. Now we need to write the add test data method.
We'll do that below configure. We'll just make this a static void add test data, which takes hotel API context. I have some data in the clipboard that I'm going to paste in here. This code will add two room entities to the context, and then we need to call cotext.savechanges. Right now, visual studio is complaining that there's an error because we haven't actually told the hotel API context what type that Db set is. We need to import the models name space to make sure this hotel API context knows what type this rooms Db set is supposed to take.
And if we switch back to startup, the error should be gone. And when we startup this application, if we take a look at the output log, if we scroll up a little bit, we'll see a message that says two entities were saved to the in-memory store. This means our database has some test data in it that we can use. Next, we'll create a route to get a room by it's ID.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Deploying ASP.NET Core Applications (2017)with Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.