我使用Repository模式以1:1的关系拥有“UserProfile”和“Executive”对象。UserProfile是系统的必需/一部分,而执行人员基于UserProfile。
这是我的UserProfile:(相关部分)
public int? ExecutiveId { get; set; }
[ForeignKey("ExecutiveId")]
public virtual Executive Executive { get; set; }
这是我的主管:(相关部分)
public int UserProfileId { get; set; }
[ForeignKey("UserProfileId")]
public virtual UserProfile UserProfile { get; set; }
这是我的FluentApi:(因为高管是可选的,需要UserProfile)
modelBuilder.Entity<Executive>()
.HasRequired(x => x.UserProfile)
.WithOptional(s => s.Executive)
.WillCascadeOnDelete(false); <-- I've tried 'true' here as well
现在,当我尝试删除它,并在SQL Mgr中查找时,我看到我删除的特定执行人员的UserProfile仍然是一个整数,而不是预期的NULL……下面是我要删除的代码:
var executive = _executiveRepository.GetExecutiveById(id);
// remove pic/content
_contentRepository.DeleteContent(executive.ProfilePictureContent.ContentGuid);
// remove mappings
_executiveSectionMappingRepository.DeleteExecutiveSectionMappingRange(executive.ExecutiveSectionMappings);
// remove executive
_executiveRepository.DeleteExecutive(id);
// remove reference from User also as EF isn't doing this the way it's setup now.
var u = _userRepository
.GetUserProfiles()
.FirstOrDefault(x => x.ExecutiveId == executive.UserProfileId);
if (u != null)
{
u.ExecutiveId = null;<-- these don't work !!
u.Executive = null; <-- not working
}
// save
_contentRepository.Save();
_executiveSectionMappingRepository.Save();
_executiveRepository.Save();
_userRepository.Save();
我错过了什么?