Mapping EF child collection of entities with Identity
See original GitHub issueI am having root entity in EF that has a few children. Some children have explicit keys and they mapped/populated as expected.
One child has Identity primary key, and it is not mapped and cause errors:
The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded.
My code is just
var entity = await dataContext.MapAsync<RootEntity>(dataModel);
In dataModel I have fields populated
“ChildrenWithIdentity”: [ { “Id”: 0, “ItineraryId”: 73551, “PopulatedProperty”: “EzgybP6Y4qApGZYyA0Hqvw==”, } ] but in mapped entity key is also 0 ,but properties are not copied “ChildrenWithIdentity”: [ { “Id”: 0, “ItineraryId”: 0, //Properties are null } ]
Do you support child entities with GeneratedOption.Identity? If yes, can you suggest what else can be wrong with my ChildrenWithIdentity ? Is any tracing can be enabled that explain why mapping for this child doesn’t work ?
Tor now, I’ve excluded ChildrenWithIdentity property by calling entity.ChildrenWithIdentity .Clear(),and process the children manually. Can you suggest other way to exclude children properties from map?
Example of my schema
public class RootEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ItineraryId { get; set; }
//Navigation Properties
public List<WorkingChild> WorkingChildren{ get; set; }
public List<ChildWithIdentity> ChildrenWithIdentity{ get; set; }
} For children that have known keys MapAsync works ok, But for table with autogenrated ID it creates object with no properties
public class WorkingChild { [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.None)]//No IDENTITY public int ComponentId { get; set; } public int ItineraryId { get; set; } //other properties } public class ChildWithIdentity {
[Key, Required,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id{ get; set; }
public int ItineraryId { get; set; } //other properties }
Issue Analytics
- State:
- Created 8 months ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Hi @MNF Good that you was able to make it work with EF-Core-Simple-Graph-Update. Sorry, there is 0 budget for this project and it is out of my possibilities to reconstruct issues from narratives or pieces of code. If you are interested in trying again, please send me your model classes and I will gladly make it work for you.
For models under any NDA you may send it directly to me by email. Closing, for the moment…
also, to exclude a property from being mapped you can use [NotMapped] atrribute or
fluent configuration
sorry for the lack of info, the project grown a lot and I’m building a wiki to collect all of these questions, but need at least another month to finish it.
you may start reading something [here] (https://github.com/leonardoporro/Detached-Mapper/wiki/Mapping-Graphs-(DTO-to-Entity)
please come back later, there will be much more info