2014-10-01 2 views
1

나는 UserProfile 엔터티와 1-1 관계를 가지고 있습니다.
MVC 코드 첫 번째 - 계단식 삭제 1 : 1 관계

사용자 엔티티 및 구성

public class User 
{ 
    public int ID { get; set; } 
    public virtual Profile Profile { get; set; } 
} 

public class UserConfig : EntityTypeConfiguration<User> 
{ 
    public UserConfig() 
    { 
     HasRequired(x => x.Profile).WithRequiredPrincipal(x => x.User); 
    } 
} 

프로필 엔티티와 CONFIGS 그러나

public class Profile 
{ 
    public int ID { get; set; } 
    public virtual User User { get; set; } 
} 

public class ProfileConfig : EntityTypeConfiguration<Profile> 
{ 
    public ProfileConfig() 
    { 
     HasRequired(x => x.User).WithRequiredDependent(x => x.Profile).Map(x => x.MapKey("UserID")).WillCascadeOnDelete(true); 
    } 
} 

내가 아래에이 예외 얻을 사용자 목록 삭제하려고 :

List<User> users = _user.Where(x => m.SelectedUsersID.Contains(x.ID)).ToList(); 
users.ForEach(x => _user.Remove(x)); 

A relationship from the 'Profile_User' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Profile_User_Source' must also in the 'Deleted' state.

나는, 당신은 사용자를 삭제 :

답변

0

좋아, 내가 두 엔티티를 모두 삭제하여 해결할 수 있습니다. 그게 내가이 너무 밖으로 시도 내가 Profile을 삭제했다 의미 User 모두

users.ForEach(x => _profile.Remove(x.Profile)); 
users.ForEach(x => _user.Remove(x)); 
0

도와주세요 무엇이 잘못되었는지 아무 생각 없어했지만 캐스케이드 삭제에 사용자가 구성되지 않았습니다. 즉, 프로필이 삭제되지 않고 데이터베이스의 무결성을 위반하게됩니다. EF는 데이터베이스를 방해하므로 오류가 발생합니다.

삭제를 계단식으로 구성해야합니다.

+0

:'HasRequired (X => x.Profile) .WithRequiredPrincipal (X => x.User) .WillCascadeOnDelete (TRUE);'하지만, 나는 여전히 같은 오류가있다. 어쨌든 나는 대답을 발견하고 위에 쓴, 큰 감사합니다 – mhesabi

관련 문제