Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

 ̄綄美尐妖づ 提交于 2019-11-30 17:40:26

No there is no such equivalent. You must use fluent API to remove cascade delete selectively or you must remove OneToManyCascadeDelete convention to remove it globally.

Create a mapping class (the fluent syntax) and use the code below:

// add relationships "Post" and "User" to a "Comment" entity
this.HasRequired(t => t.Post)
    .WithMany(t => t.Comments)
    .HasForeignKey(d => d.PostID)
    .WillCascadeOnDelete(false); // <---

this.HasOptional(t => t.User)
    .WithMany(t => t.Comments)
    .HasForeignKey(d => d.UserID)
    .WillCascadeOnDelete(false); // <---

Here's a nice post on how to set up fluent mappings if you need more info.

Just make the FK property nullable can prevent cascade delete from happening:

public int? OutputTypeID { get; set; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!