Can't Update DB after following Data Access Documentation
I am following this document and am completely stuck with updating items in the DB.
I am able to retrieve the data that is seeded from the DbInitializer class., but cannot add new students or edit existing ones. Normally I would search the error output but I am not getting anything or in the console either.
I've compared my code with theirs and even replicated it with this repo with no luck.
One thing I noticed is the program.cs is different than what gets generated by Visual Studio. Also there is no startup.cs that gets generated, it seems like that is all handled in the program.cs?
Any help would greatly be appreciated.
Tutorial: Implement CRUD Functionality - ASP.NET MVC with EF Core
In this tutorial, you'll review and customize the CRUD (create, read, update, delete) code that the MVC scaffolding automatically creates for you in controllers and views.
GitHub
test/test at master · jessig1/test
Contribute to jessig1/test development by creating an account on GitHub.
18 Replies
Also there is no startup.cs that gets generated, it seems like that is all handled in the program.cs?correct, modern versions of ASP.NET Core do all the setup in Program.cs at a glance your EF specific code looks right, have you debugged to make sure it's actually running?
I'm probably missing something, but yes I am running in debug mode and even tried adding some additional logging, but maybe I'm looking in the wrong spot?
you sure you are looking in the correct database? connection string says localdb, while there is a container showing on the screen
I think so I believe that was an old DB I tried to run via container, but I think it's using a DB running on IIS?
looks correct, set a breakpoint in the controller methods and make sure they are called
I set the breakpoint at the student edit method and the browser closed, does that mean the method is getting called? (sorry for the dumb questions)
no, maybe you stopped the program
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
you should set a breakpoint on the if statements checking ModelState.IsValid and seeing what the value of that is
without any other information, i'm guessing you are submitting invalid data to your API and it's just not making any changes to the db
I think you're right if I am looking at this right, the model is invalid causing it to not write?
probably
looks like specifically the enrollments property failed to validate
Thank you both for your help!
also, as a side note it's generally not recommended to use EF models for anything other than interacting with the database
in this example you'd have a different Student class to represent the API model and map back and forth as needed
otherwise it quickly gets messy when your database model needs to look different from your api
That's good to know. This is literally from M$ documentation...you think they'd have better up to date info and best practices...
Any resources you would recommend? Their docs can be really frustrating.
#database here is good for general questions, i don't have any specific projects or guides to reference
Got it working, issue was with the enrollments model. Thank you for the help, I was going insane tryign to figure this out
Add some "?" to the model?
I had the same problem and I added some question marks and it worked, but I don't know if it's safe to do so
Yes that was the issue