m4gik
m4gik

Reputation: 450

How to Delete Orphaned Records

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;
            }
        }

Upvotes: 4

Views: 168

Answers (1)

Jonathan Magnan
Jonathan Magnan

Reputation: 11347

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.

Upvotes: 2

Related Questions