question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Orphans are deleted when overlapping composite alternate key is mutated

See original GitHub issue

Given 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 fromParent.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:closed
  • Created a year ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mrpmorriscommented, Sep 10, 2022

Thanks. To vote, do I just thumb it up?

0reactions
mrpmorriscommented, Sep 12, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found