C
C#17mo 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
mindhardt17mo ago
mindhardt
mindhardt17mo ago
mindhardt
mindhardt17mo ago
Current solution does 2 queries 😦
phaseshift
phaseshift17mo 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
mindhardt17mo 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
phaseshift17mo ago
That makes no sense since theyre already mixed together...
mindhardt
mindhardt17mo ago
My Web proj depends on DnD proj, to add back property in CharacterSnapshot I would need circular reference
phaseshift
phaseshift17mo 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
Accord17mo 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
More Posts