C
C#2y ago
mindhardt

❔ ✅ EFCore: How to add entity with one-to-many relationship without navigation property in 1 query

In my ApplicationUser class I have Characters property (see screenshots), but Character does not have a back navigation property. I want to add Character into a database and attach it to a user with specified Id in one query. Any tips with LINQ?
9 Replies
mindhardt
mindhardtOP2y ago
mindhardt
mindhardtOP2y ago
mindhardt
mindhardtOP2y ago
Current solution does 2 queries 😦
phaseshift
phaseshift2y ago
You can include the fk id as a property on charactersnapshot, the just added the charactersnapshot to it's own dbset after setting the fk value I think you can also set the shadow property value of the fk if you don't have the fk property in your entity
mindhardt
mindhardtOP2y ago
Shadow properties seem like a way to go, thanks That is what I don't want to do since CharacterSnapshot is a class from my own yet external library that has nothing to do with this code
phaseshift
phaseshift2y ago
That makes no sense since theyre already mixed together...
mindhardt
mindhardtOP2y ago
My Web proj depends on DnD proj, to add back property in CharacterSnapshot I would need circular reference
phaseshift
phaseshift2y ago
No, you only need to add a property for the id, e.g a guid or long, etc But if you've got entity classes in different projects that's weird already
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server