4

I'm trying to use GraphDiff and Entity Framework to update a set of records across multiple tables. Everything works except I need to delete the orphaned record for any of the owned entities that may have been replaced. What am I missing as I expect that this behavior is common and I just need to figure out how to configure the context or graph appropriately. Here is my sample code:

        using (EfDataContext ctx = new EfDataContext())
        {
            try
            {
                ctx.Database.Log = msg => _sysLogObject.Debug(msg);

                ctx.UpdateGraph(assay, map => map
                    .OwnedCollection(p => p.Imagings, with => with
                        .OwnedEntity(p => p.ImagingCellType))
                    .OwnedEntity(p => p.DisplayTemplate)
                    .OwnedEntity(p => p.ExportTemplate)
                    .OwnedEntity(p => p.PrintTemplate)
                );
                ctx.SaveChanges();
                success = true;
            }
            catch (Exception ex)
            {
                _sysLogObject.Error(ex);
                throw;
            }
        }
m4gik
  • 430
  • 5
  • 27

1 Answers1

2

Disclaimer: I'm the owner of the project Entity Framework GraphDiff

We also got the same question by email. The answer was:

The child must have a navigation property toward the parent to make it work. Otherwise, the entity is just skipped by the ChangeTracker from Entity Framework.

Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60