C# TBH EF核心中的可选外键
我想在EF核心的TBH(每个层次结构的表)中使用可选外键。我尝试了许多解决方案,但都不奏效。我想在删除其他实体时将外键设置为null。在我的代码中,这意味着当删除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
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?谢谢你的帮助