Orphans are deleted when overlapping composite alternate key is mutated
See original GitHub issueGiven the following simple Parent
/Child
model, where Child
is an aggregate part of Parent
(and cannot exist without it).
internal class Parent
{
public Guid Id { get; private set; } = Guid.NewGuid();
public virtual List<Child> Children { get; private set; } = new();
}
internal class Child
{
public Guid Id { get; private set; } = Guid.NewGuid();
public Guid ParentId { get; set; }
public virtual Parent Parent { get; set; } = null!;
}
I want to prohibit a Parent
from being deleted if it has Children, and I also want any
Childthat is removed from
Parent.Children` to be automatically deleted on save.
internal class ApplicationDbContext : DbContext
{
public DbSet<Parent> Parents { get; set; } = null!;
private DbSet<Child> Children { get; set; } = null!;
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Child>()
.HasOne(x => x.Parent)
.WithMany(x => x.Children)
.OnDelete(DeleteBehavior.Restrict);
}
}
As per the documentation, this code throws an exception because Child.Parent
cannot be null
.
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase("Test")
.Options;
using (var context1 = new ApplicationDbContext(options))
{
var parent = new Parent();
parent.Children.Add(new Child());
context1.Parents.Add(parent);
context1.SaveChanges();
}
using (var context2 = new ApplicationDbContext(options))
{
var parent = context2.Parents.Include(x => x.Children).First();
parent.Children.RemoveAt(0);
context2.SaveChanges();
}
It seems that modelBuilder.Entity<Child>().HasAlternateKey(x => new { x.Id, x.ParentId })
has the behaviour I expected, but @ajcvickers said “I certainly wouldn’t do it that way” - https://twitter.com/ajcvickers/status/1565633141880102914
What is the recommended practice to ensure the child is deleted instead?
Issue Analytics
- State:
- Created a year ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Genetics Exam 3 (ch. 16-21) Flashcards
Study with Quizlet and memorize flashcards containing terms like The Ames test determines the frequency with which a chemical causes mutations in DNA....
Read more >22q11.2 Deletion Syndrome - GeneReviews
Once the 22q11.2 deletion has been identified in an affected family member ... Key Clinical Features Overlapping w/22q11.2 Deletion Syndrome ...
Read more >COL1-Related Disorders: Case Report and Review ...
The reported case and the literature review suggest that the COL1-related overlap disorders (OI, EDS and overlapping syndromes) represent a ...
Read more >Work with alternate keys (Microsoft Dataverse) - Power Apps
If the alternate key is deleted while an index creation job is still pending or in progress, the job is cancelled and the...
Read more >A Cohort Study of Patients With Hypersensitivity to Vitamin D
Overlapping Phenotypes Associated With CYP24A1, SLC34A1, and SLC34A3 Mutations: A Cohort Study of Patients With Hypersensitivity to Vitamin D.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks. To vote, do I just thumb it up?
I really hope the priority of that report goes up. Otherwise it’s a lot of additional work to use EF for a DDD approach.