question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to map a child item when creating a parent item? (not a bug more a question)

See original GitHub issue

Is it even possible like this?

TestDbContext db = await TestDbContext.CreateAsync();

Role role = new Role() { Name = "Test" };
db.Roles.Add(role);
await db.SaveChangesAsync();

// Create user and assign existing role
User user = new() { Name = "Example" };
user.Roles.Add(role);

await db.MapAsync<User>(user);

await db.SaveChangesAsync();

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
leonardoporrocommented, Nov 13, 2021

So, the mapper works with two concepts, the DTO and the Entity which may be the same type, or not.

For example:

  • User to User map / dto: User and entity: User.
  • UserDTO to User map / dto: UserDTO and entity User.

Mapper then will:

  • Load root Entity along with [Compositon] children recursively and only the Id of [Aggregation] children (the graph).
  • Start traversing the graph and copying the properties. For [Aggregation]
  • Copy only the Id
  • Mark the Entity as Unchanged.
  • Do not recurse! Aggregations end here. For [Composition]
  • If DTO exists and Entity doesn’t, mark as Added
  • If DTO doesn’t exist and Entity does, mark as Deleted
  • If both exists, mark as Modified.
  • Continue recursively with the rest of the Complex or Collection properties.

[Aggregation] are supossed to be independent entities/graphs that the entity is referring to. And shouldn’t be added/deleted along with it. e.g.: Invoice -> InvoiceType That’s why only Id is mapped for aggregations and is marked as Unmodified. If the aggregated entity doesn’t exists, an fk error is thrown when saving.

[Composition] are entities that form part of the same concept and should be updated/deleted with the root entity. e.g.: Invoice -> InvoiceDetail.

Another interesting thing. Given the previous DTO and Entity definitions, values are always copied, even when they are the same type. The User that you passed to the MapAsync method is not the same that gets attached to the context. The attached one comes from the initial query or is instantiated by the mapper, and it’s returned by the MapAsync method. var attachedUser = await db.MapAsync<User>(detachedUser); In this example, attached and detached users are not the same instance.

In this your case, MapAsync will:

  • Load the root, but the key is empty so it will create a new User.
  • Copy Id, Name, DoB and other simple values
  • Recursively iterate Roles
  • There will be 1 role DTO and 0 role Entities, so it will create a Role and since it is an [Aggregation] it will copy the Id and mark it as Unmodified.

This is a very high level description, as there much more, like circle dependencies (BackReference in the code), type configuration, mapping configuration, the dynamic code, etc.

Feel free to ask on a certain topic if you are interested on it.

0reactions
MaRRiK74commented, Nov 13, 2021

Ah the return value, thanks for pointing that out! With that I can work something out. Many thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you parent a bug under an epic? - azure devops
Use the mapping pane in the backlog to map a bug to an epic (this also is only designed to allow you to...
Read more >
CSV import Parent-child mapping | Jira
Firstly, make sure the exported CSV file contains at least the following columns for the parent-child mapping: Issue type, Issue key, ...
Read more >
Resolve nest, display, and reorder issues for work items
You can quickly map product backlog items to features, which creates parent-child links in the background. Don't create a hierarchy of backlog ...
Read more >
Link work items and other objects - Azure Boards
To quickly link backlog items to portfolio backlog items with parent-child links, use the mapping pane to organize your backlog. · To create...
Read more >
All about subitems
To convert a subitem to an item on your board, select a subitem from the left side and click "Convert" on the bottom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found