EF Code First Fluent API - 级联删除 [英] EF Code First Fluent API - Cascade Delete

查看:197
本文介绍了EF Code First Fluent API - 级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:

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


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

用户是主表,关系是一对一的。一个用户只有一个UserProfile。

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

如何使用EF CodeFirst Fluent API定义User和UserProfile之间的关系,以便当我从User表中删除一个用户时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?

推荐答案

使用 WillCascadeOnDelete

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

这篇关于EF Code First Fluent API - 级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆