New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to map a child item when creating a parent item? (not a bug more a question) #41
Comments
Yes it is. I double checked just in case, this is the working code: 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 = new List<Role>(); // null reference here! in the old code.
user.Roles.Add(role);
await db.MapAsync<User>(user);
await db.SaveChangesAsync();
// result will contain an user with the assigned role
var result = db.Users.Where(u => u.Name == "Example").FirstOrDefault();
|
Thanks for the quick response. The null ref was not the issue on my side (my model always creates a new list). But now I am facing the issue that after saving the dbcontext, the PK of the parent Item is always '0' in this case the user.Id is always 0. Is there an option to tell detached-mapper to return the value which was assigned? |
So, the mapper works with two concepts, the DTO and the Entity which may be the same type, or not. For example:
Mapper then will:
[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 [Composition] are entities that form part of the same concept and should be updated/deleted with the root entity. 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. In this your case, MapAsync will:
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. |
Sorry, we were writting posts at the same time, but I anwsered your question over there. |
Ah the return value, thanks for pointing that out! With that I can work something out. Many thanks again. |
Is it even possible like this?
The text was updated successfully, but these errors were encountered: