'How to stablish on delete action but skiping on severed (set to null)

Here are the classes example

public class Parent
{
    public int ParentId { get; set; }
    public string Name { get; set; }
    public Child Child { get; set; }
}

public class Child
{
    public int ChildId { get; set; }
    public string Note { get; set; }
    public int? ParentId { get; set; }
    public Parent Parent { get; set; }
}

here is the Fluent configuration

modelBuilder.Entity<Child>()
            .HasOne(x => x.Parent)
            .WithOne(x => x.Child)
            .OnDelete(DeleteBehavior.Cascade);

and here is the operation.

var p = await _context.Parents
          .Where(x => x.ParentId == id)
          .Include(x => x.Child)
          .FirstOrDefaultAsync();

var p2 = await _context.Parents
          .Where(x => x.ParentId == id2)
          .Include(x => x.Child)
          .FirstOrDefaultAsync();

//Below works just fine, remove the parent and child
_context.Parents.Remove(p);

//In this two is where I want to conserve the child in DB.
p2.Child = null;
//Or
p2.Child = new ();

_context.Update(p2);
_context.SaveChanges();

What I'm looking for is: to delete the dependent side when I delete the Principal, Done. If I set null the Child in the Parent, preserve the Child.

Any clue on this is welcome, thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source