Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TBH EF核心中的可选外键_C#_Entity Framework_Inheritance - Fatal编程技术网

C# TBH EF核心中的可选外键

C# TBH EF核心中的可选外键,c#,entity-framework,inheritance,C#,Entity Framework,Inheritance,我想在EF核心的TBH(每个层次结构的表)中使用可选外键。我尝试了许多解决方案,但都不奏效。我想在删除其他实体时将外键设置为null。在我的代码中,这意味着当删除FriendRequest时,我想将FriendRequestNotification中的FriendRequest设置为null 我总是收到以下错误: Npgsql.PostgresException:23503:表“FriendRequests”上的更新或删除违反了表“Notifications”上的外键约束“FK_Notifica

我想在EF核心的TBH(每个层次结构的表)中使用可选外键。我尝试了许多解决方案,但都不奏效。我想在删除其他实体时将外键设置为null。在我的代码中,这意味着当删除
FriendRequest
时,我想将
FriendRequestNotification
中的
FriendRequest
设置为null

我总是收到以下错误:

Npgsql.PostgresException:23503:表“FriendRequests”上的更新或删除违反了表“Notifications”上的外键约束“FK_Notifications\u FriendRequests\u FriendRequestId”

DbContext

 public class HankContext : DbContext
 {
    public virtual DbSet<Notification> Notifications { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Notification>()
            .HasDiscriminator<NotificationType>("Type")
            .HasValue<FriendRequestNotification>(NotificationType.FriendRequest)
            .HasValue<FriendAcceptNotification>(NotificationType.AcceptedFriend);

        //this part is the one which I thought would solve the problem
        builder.Entity<FriendRequest>()
               .HasOne<FriendRequestNotification>()
               .WithOne(x => x.FriendRequest)
               .IsRequired(false)
               .OnDelete(DeleteBehavior.SetNull);
      }
    }
通知

public class FriendRequestNotification : Notification
{
    public Guid? FriendRequestId { get; set; }
    public virtual FriendRequest FriendRequest { get; set; }
}
public abstract class Notification
{
    public Guid Id { get; set; }

    [Required]
    public NotificationType Type { get; set; }

    [Required]
    public DateTime CreatedAt { get; set; }

    [Required]
    public User Receiver { get; set; }

    [Required]
    public bool IsRead { get; set; }
}
如何将上述外键设置为null?谢谢你的帮助