简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM