Client cascade delete for owned entities

Open
#10,179 13 comments 3 reactions 1 assignee View on GitHub

@AndriySvyryd is already working on this.

Since Nov 2, 2017.

Assessment

This issue has not been assessed yet.

Description

area-change-tracking area-owned-entities

I am trying to delete entities just by giving the PK however it fails if the entity contains Complex Type fields

Example

    public class BowtieCauseDetails
    {
        [Key]
        public int NodeId { get; set; }

        public BowtieCauseTimeFields Current { get; set; }

        public BowtieCauseTimeFields Proposed { get; set; }
    }

    public class BowtieCauseTimeFields
    {
        public BowtieNodeInOutFrequencyFields Outgoing { get; set; }
    }

    public class BowtieNodeInOutFrequencyFields
    {
        public decimal Frequency { get; set; }
    }

Example

            var ctx = new MyDbContext();

            var toBeDeleted = ctx
                .BowtieCauseDetails
                .AsNoTracking()
                .Select(e => new BowtieCauseDetails
                {
                    NodeId = e.NodeId // PK only
                })
                .ToList();

            ctx.BowtieCauseDetails.RemoveRange(toBeDeleted);

            ctx.SaveChanges();

It throws the following exception

Unhandled Exception: System.InvalidOperationException: The entity of 'BowtieCauseDetails' is sharing the table 'BowtieCauseDetails' with 'BowtieCauseDetails.Current#BowtieCauseTimeFields', but there is no entity of this type with the same key value that has been marked as 'Deleted'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.

If I provide empty place holder for those fields, it still fails but with a different message

Example

            var ctx = new MyDbContext();

            var toBeDeleted = ctx
                .BowtieCauseDetails
                .AsNoTracking()
                .Select(e => new BowtieCauseDetails
                {
                    NodeId = e.NodeId,
                    Current = new BowtieCauseTimeFields
                    {
                        Outgoing = new BowtieNodeInOutFrequencyFields
                        {
                        }
                    },
                    Proposed = new BowtieCauseTimeFields
                    {
                        Outgoing = new BowtieNodeInOutFrequencyFields
                        {
                        }
                    }
                })
                .ToList();

            ctx.BowtieCauseDetails.RemoveRange(toBeDeleted);

            ctx.SaveChanges();

Unhandled Exception: System.InvalidOperationException: The property 'BowtieCauseTimeFieldsBowtieCauseDetailsNodeId' on entity type 'BowtieCauseDetails.Current#BowtieCauseTimeFields.Outgoing#BowtieNodeInOutFrequencyFields' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.

However if I get at least one field from the DB then it works

Example

            var ctx = new MyDbContext();

            var toBeDeleted = ctx
                .BowtieCauseDetails
                .AsNoTracking()
                .Select(e => new BowtieCauseDetails
                {
                    NodeId = e.NodeId,
                    Current = new BowtieCauseTimeFields
                    {
                        Outgoing = new BowtieNodeInOutFrequencyFields
                        {
                            Frequency = e.Current.Outgoing.Frequency
                        }
                    },
                    Proposed = new BowtieCauseTimeFields
                    {
                        Outgoing = new BowtieNodeInOutFrequencyFields
                        {
                            Frequency = e.Current.Outgoing.Frequency
                        }
                    }
                })
                .ToList();

            ctx.BowtieCauseDetails.RemoveRange(toBeDeleted);

            ctx.SaveChanges();

I think it should work by giving the PK only.

Here is the full sample for easier replication

using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace EfTests
{
    class Program
    {
        static int Main(string[] args)
        {
            var ctx = new MyDbContext();

            var toBeDeleted = ctx
                .BowtieCauseDetails
                .AsNoTracking()
                .Select(e => new BowtieCauseDetails
                {
                    NodeId = e.NodeId,
                    //Current = new BowtieCauseTimeFields
                    //{
                    //    Outgoing = new BowtieNodeInOutFrequencyFields
                    //    {
                    //        Frequency = e.Current.Outgoing.Frequency
                    //    }
                    //},
                    //Proposed = new BowtieCauseTimeFields
                    //{
                    //    Outgoing = new BowtieNodeInOutFrequencyFields
                    //    {
                    //        Frequency = e.Current.Outgoing.Frequency
                    //    }
                    //}
                })
                .ToList();

            ctx.BowtieCauseDetails.RemoveRange(toBeDeleted);

            ctx.SaveChanges();

            return 0;
        }
    }

    public class BowtieCauseDetails
    {
        [Key]
        public int NodeId { get; set; }

        public BowtieCauseTimeFields Current { get; set; }

        public BowtieCauseTimeFields Proposed { get; set; }
    }

    public class BowtieCauseTimeFields
    {
        public BowtieNodeInOutFrequencyFields Outgoing { get; set; }
    }

    public class BowtieNodeInOutFrequencyFields
    {
        public decimal Frequency { get; set; }
    }

    public class MyDbContext : DbContext
    {
        public DbSet<BowtieCauseDetails> BowtieCauseDetails { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=localhost,1433;Database=ipl_tux;Integrated Security=True;");            
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BowtieCauseDetails>()
                .OwnsOne(c => c.Current)
                .OwnsOne(c => c.Outgoing);

            modelBuilder.Entity<BowtieCauseDetails>()
                .OwnsOne(c => c.Proposed)
                .OwnsOne(c => c.Outgoing);

            base.OnModelCreating(modelBuilder);
        }
    }
}
Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.