Simple Example for DTO to Entity mapping for one to many relationship #108
Unanswered
millionpixeldfi
asked this question in
Q&A
Replies: 2 comments
-
no |
Beta Was this translation helpful? Give feedback.
0 replies
-
Composition test would be the simpler example, here About your issue, I guess that it's not recognizing FolderId and FileId as keys.. will add a ticket to check on this. You may try with just Id if you want. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi I have the following DB-Entity model in EF:
Folder (FolderId, FolderName, Files)
File(FileId, FileName)
Folder contains a NOT nullable list of files so this would be a one to many relationship.
When I now load a folder from the DB, I map everthing into a FolderDTO which has a list of File DTO's.
Then I try to Change the FolderName and would like to update the Folder but then I'am getting an error when I try to map back from DTO to Entity:
var updatedFolder = context.Map<Folder>(folderDto);
System.InvalidOperationException: 'The instance of entity type 'File' cannot be tracked because another instance with the same key value for {'FileId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.'
What I'm doing wrong ? I was not able to find a good example on how to map such basic relationships in particular when I would delete a FileDTO, is then Detached-Mapper able to figure out which entity was deleted ?
Beta Was this translation helpful? Give feedback.
All reactions