Client cascade delete for owned entities
@AndriySvyryd is already working on this.
Since Nov 2, 2017.
Assessment
This issue has not been assessed yet.
Description
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100