Client cascade delete for owned entities
See original GitHub issueI 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);
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:13 (9 by maintainers)
You can fix it with a simple model configuration. Add this to your DbContext class:
Now, all the owned types (complex objects) will be deleted automatically when you delete the owner entity.
@ralmsdeveloper - This is different from #10180 because here there is no need of unique indexes.
The issue here is, when you are trying to delete an entity it would remove the row from database. But since the table is being shared, there would be other entities using the same row so unless all of the entities sharing the row are deleted, deleting the row could be incorrect and cause data corruption. Hence before sending command EF Core tried to verify if all the entities in that row is marked as deleted and in the absence threw above exception.
Though in this particular case, owned entities are sharing the table. If owner is deleted then owned entities should also lose its existence since it is tied with owner. If the entities are loaded in different state then its an error. But if it not then owner is deleted, owned children could be deleted too.
Another use of table sharing is identifying 1-to-1 relationship. In that case, we need to think about the effects. For the case of principal getting deleted with cascade delete is enabled, we can delete but not for all cases.