EF Code First Fluent API - Cascade Delete

一世执手 提交于 2019-12-08 05:06:37

问题


I have two models:

public class User
{
   .....
   public virtual UserProfile UserProfile { get; set;}
}


public class UserProfile
{
   .....
   public virtual User User { get; set;}
}

The User is the master table and the relation is one to one. One user has only one UserProfile.

How can I define the relationship between User and UserProfile using EF CodeFirst Fluent API in such a way that when I delete one user from User table the user profile from Userprofile is also deleted?


回答1:


Use WillCascadeOnDelete

modelBuilder.Entity<UserProfile>()
    .HasKey(c => c.Id)
    .HasRequired(c => c.User)
    .WithRequiredDependent(c => c.UserProfile)
    .WillCascadeOnDelete(true);


来源:https://stackoverflow.com/questions/25114703/ef-code-first-fluent-api-cascade-delete

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