✅ Can you add related data to a DbContext like this
I have these classes where Company.Locations navigates to a Locations table and Location.LocationReviews navigates to a LocationReviews table
I have a generic controller that Im now finding out only works for Company entities.
26 Replies
If I put in a Location or LocationReview, I get an error like this.
The way I feel like it's supposed to work is when I put in a LocatonReview, it will check the Location and Company it belongs to and then add it. The problem is, it is acting as if I'm trying to add a new Company and Location. Am I doing something wrong, is there a work around?
Here is the JSON of what I'm trying to add if it helps
of course something like this isn't going to work when you use ef incorrectly
Can you provide guidance then please? Where my mistakes are and how to fix them?
A generic endpoint like this simply isn't going to work for what you want
I mean, every method except save already works
What exactly doesn’t work
"except save" oh so it doesn't work
I wasn’t disagreeing, I’m just asking what is about save that I’m doing incorrectly
Just calling Update on EF is almost never advised. As you can see here, its going to attempt to insert new entities instead of updating something you think should be updated. EF isn't going to check if all your child entities exist, it figures because they are not tracked, you are adding them
Thanks for the advice. What is the better way to do it?
Nothing generic if your expecting to pass over all that json.
I can get rid of the generics. They just let me only have to write one data access class
well you'd want to load in the relevant data, update anything that existed if this is going to accept updating/adding multiple locations/reviews, and insert anything new
Are you saying I should do this? Or something different?
I feel like this should be able to be simplified
Are you only trying to add a single location or review at a time here
Yes
Then I guess you could simplify this by adding the LocationId or CompanyId properties onto your entities like they should already have, and pass that over instead of the respective object
Good idea. Would this be sufficient without have to access the company
you'd probably even be able to just use the generic method if you really wanted to.
if you attempt to add a review, its going to bitch if the locationid doesn't actually exist assuming you have a fk relation setup
How would the database be set up in this example. Would I have to tell the modelBuilder that LocationId is a foreign key. Would there be no relationship at all and I just query the Location it belongs to if I need it?
your database should already have that field, you just didn't have it on your model. You had the Location object on it, which EF will know about that relationship from the LocationId that it uses in the database. Having LocationId and Location properties on your model ef will know about the relationship by convention
Then the only real difference would be that the model now has a reference to the id instead of the Location itself?
you'd put both properties on the model so the relationship is there to be used in code
So ?
yeah
Okay appreciate all the help
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.