【发布时间】:2022-01-22 02:11:30
【问题描述】:
public class Email
{
public int Id { get; set; }
/// ...
public int? ReplyTo { get; set; }
public int? ForwardOf { get; set; }
}
我想通过级联删除将ReplyTo 和ForwardOf 配置为FK 到Email.Id 属性。
试过这个:
e.HasOne(nameof(Email.ReplyTo)).WithMany().OnDelete(DeleteBehavior.Cascade);
但它给出了一个错误
The specified type 'System.Nullable`1[System.Int32]' must be a non-interface reference type to be used as an entity type.
我不希望有Email 类型的导航属性,因为我的代码永远不会使用它们。
【问题讨论】:
-
您是否尝试过将
int?替换为Email以便EF 可以自行构建FK?我通常以这种方式做类似的事情:public class Package { public int Id { get; set; } public Package Parent { get; set; } }这样,如果没有相关项目,数据库中的 FK 值将只是 0。我认为在这种情况下您仍然可以级联删除
标签: entity-framework entity-framework-core ef-core-5.0