Join Alexander Zanfir for an in-depth discussion in this video Save questions with Entity Framework, part of Building Applications with Angular, ASP.NET Core, and Entity Framework Core.
- [Instructor] In this video we'll save our new question using entity framework. The first thing we need to do is create a db context. This will give us a db set to work with our questions. So let's create a new class called QuizContext. So I'll right click on the project, select Add, and Class. And I'll call it QuizContext. Inside here on line eight, in the class definition, I'll derive from and add DbContext, and if we hover over we can see we'll need to add using Microsoft Entity Framework Core.
Then let's create a constructor, and we'll take in dBContextOptions with type QuizContext. And then we'll pass those options along to the base. Then let's create the db set we talked about that will give us access to our questions. So I'll set the type to DbSet with Models.Question and we'll call it Questions.
Now that we have our QuizContext let's close that out and go over to our startup. Then let's go back up to our ConfigureServices function on line 24 and below AddCors we'll configure our context. We'll use services.AddDbContext with our type of QuizContext and we'll set some options with options.UseInMemoryDatabase.
And in order to have access to that we'll need to make sure we're using the entity framework namespace so let's go up to the top and below line 10 we'll have using Microsoft.EntityFrameworkCore. Now if we scroll back down we can see we're able to recognize this and all we need to do is name our database, and I'll call it quiz. Now that our QuizContext is setup let's go ahead and use it. We'll first save our startup then let's close it out, and inside our QuestionsController let's inject our QuizContext.
And to do that we'll need to create a constructor, and it will take in a QuizContext that we'll call quiz. And then we'll need to save that as a local reference. So let's create that in our class. We'll do that above the constructor. We'll make it read only of type QuizContext called context. Then we'll set this.context to context.
Now that we have reference to our QuizContext we can access our questions and we can save to it and read from it. So first let's try saving with it. Inside our Post function I'll take off the break point and I'll call context.Questions.add. We'll add a placeholder question instead of the question that's passed in from our front end for now just to isolate this test.
And let's initialize it. Let's save this and try to run it. So let's go back to Postman, make sure it's set up correctly, and then hit Send. And as we can see our execution stops because of an exception. It's telling us that the entity type Question requires a primary key to be defined and that's because we need a way to identify each question. So let's stop our server and open up our questions model.
Then let's give it an id. So at the top I'll add in a new property with type int called ID. Let's save that and close it out and let's try it again. Once again with Postman I'll hit Send and we're getting a 200 OK. And then let's run our server without debugging so I'll go over to the Debug menu and hit Start Without Debugging.
The shortcut to this is Ctrl + F5. If you were running the server in this mode and you tried to post with Postman there is a good chance that you might get another Cors error. But how can this be since we've already enabled Cors? The reason is when the server gets a 500 internal server error it might respond earlier than when our Cors policy is added.
So any time that you might get a Cors error it might just be an internal server error and at that point instead of running it without debug make sure you run it in debug and then you'll see the actual error. So let's go ahead and undo our comment inside our Questions.cs and close that out. Next, to actually save the database once we have modified it, we have to call SaveChanges on the context. So below line 33, in our post function, let's call context.SaveChanges.
Now if we run our program and we set a break point at SaveChanges and then we'll run our server. Then from our front end let's send another post. Now let's hover over our Context.Questions and look at the results view. We can see that there's no results yet. Next let's set a break point right below SaveChanges so that SaveChanges does execute and then we'll hit Continue.
If we look at the results again we can see we have one question with an ID of one and text test. So we know it's working. So, at this point, we're able to save an actual question from the front end. Next let's look at how we can add answers to our question.
- Creating and configuring the Angular project
- Creating forms with Angular
- Creating get and post routes in an ASP.NET Core controller
- Updating Angular service to post to API
- Persistence with Entity Framework
- Displaying and editing data in Angular with ASP.NET Core
- Navigating to different views in Angular
- Associations between entities with Angular and Entity Framework
- Setting up Identity Framework