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.

Mapping EF child collection of entities with Identity

See original GitHub issue

I 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:closed
  • Created 8 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
leonardoporrocommented, Feb 10, 2023

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…

1reaction
leonardoporrocommented, Jan 23, 2023

also, to exclude a property from being mapped you can use [NotMapped] atrribute or

public class RootEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ItineraryId { get; set; }

    [Composition]
    public List<WorkingChild> WorkingChildren { get; set; }

    [NotMapped]
    public List<ChildWithIdentity> ChildrenWithIdentity { get; set; }
}

fluent configuration

 optionsBuilder.UseSqlServer(@"Data Source=.;Initial Catalog=myDataBase;User ID=sa;Password=ThisIsMyPassword2023;Trust Server Certificate=true;")
                      .UseMapping(profiles =>
                      {
                          profiles.Default(cfg =>
                          {
                              cfg.Type<RootEntity>().Member(m => m.ChildrenWithIdentity).Exclude();
                          });
                      });

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to map in EF Core a collection of child entities when ...
How could I map this in EF Core using fluent API? Or is it not possible and I have to have in the...
Read more >
Introduction to relationships - EF Core
EF Core relationship mapping is all about mapping the primary key/foreign key representation used in a relational database to the references ...
Read more >
Advanced table mapping - EF Core
EF Core allows to map two or more entities to a single row. This is called table splitting or table sharing. Configuration. To...
Read more >
Handling child collections in Entity Framework - marisks # code
Configuring Entity Framework through fluent API sometimes is quite hard. In this article I am showing how to configure it to handle entity's...
Read more >
Configure One-to-Many Relationships in EF 6
Here, we will learn how to configure One-to-Many relationships between two entities (domain classes) in Entity Framework 6.x using the code-first approach.
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