Home > Enterprise >  FK to same table using nullable Id's
FK to same table using nullable Id's

Time:12-23

public class Email
{
    public int Id { get; set; }
    /// ...
    public int? ReplyTo { get; set; }
    public int? ForwardOf { get; set; }
}

I would like to configure ReplyTo and ForwardOf to be FK to Email.Id property with cascade Delete.

Tried this: e.HasOne(nameof(Email.ReplyTo)).WithMany().OnDelete(DeleteBehavior.Cascade);

but it gives an error

The specified type 'System.Nullable`1[System.Int32]' must be a non-interface reference type to be used as an entity type. I would prefer not to have navigation properties of type Email as they will never be used by my code.

CodePudding user response:

This should allow a shadow navigation property:

.HasOne<Email>()
    .WithMany()
    .HasForeignKey(x => x.ReplyTo)
    .OnDelete(DeleteBehaviour.Cascade);

Though I'm not sure you'd want a delete cascade on such a relationship.

  • Related