를 사용하여 InverseProperty 관계에서 아이 엔티티를 삭제하는 방법 :나는 모델을 가지고 내가 (간체) 엔티티 여기에 프레임 워크 4.1</p> <p>모델을 사용하여 문제의 관계를 관리하는 데 코드 첫 번째 엔티티 프레임 워크 4.1
public class UserConfig {
[Key]
[Column("id", TypeName = "bigint")]
public long Id { get; set;}
[InverseProperty("UserConfigId")]
public virtual List<ColumnConfig> ColumnConfigs { get; set; }
}
public class ColumnConfig {
[Key]
[Column("id", TypeName = "bigint")]
public long Id { get; set; }
[Column("user_config_id", TypeName = "bigint")]
public long UserConfigId { get; set; }
[Column("width", TypeName = "int")]
public int Width{ get; set; }
[Column("col_name", TypeName = "varchar")]
public string ColumnName{ get; set; }
}
을
모델은 UI 내의 데이터 테이블에 대한 사용자 및 사용자 지정보기를 나타냅니다. 열의 크기를 원하는대로 조정 한 다음 설정을 저장합니다. 원하는 열의 목록과 각각의 너비를 허용하는 webservice가 있습니다.
문제점은 웹 서비스에서 사용자의 ColumnConfigs를 업데이트하는 것입니다. webservice는 ColumnConfig ID를받지 못하기 때문에 사용자의 기존 ColumnConfigs를 모두 삭제 한 다음 전달 된 새 값에 따라 두 번째 객체를 새로 만듭니다.
ColumnConfig 객체를 삭제할 수 없습니다.
이 작동하지 않습니다public void UpdateUserConfig(UserConfig uc) {
UserConfig origUserConf = ctx.ColumnConfigs.Find(new object[] {uc.Id});
origUserConf.ForEach(uc => ctx.ColumnConfigs.Remove(uc)); // remove old
origUserConf.ColumnConfigs = uc.ColumnConfigs; // add new
ctx.SaveChanges();
}
, 그것은 오류를 제공합니다 : 여기 내 코드의 여기 널 제약이 생각하는 이유
System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
잘 모르겠어요. 필요한 필드를 지정하지 않았습니다.
케이스 (1) 내 관계 및 ForEach 제안을 통해 목록의 엔터티를 조작 할 수있었습니다. 그러나 여전히 ColumnConfigs 목록에서 자식 엔티티를 변경할 수 없습니다. 코드를 업데이트하고 내 문제의 핵심 인 새로운 예외를 보여주었습니다. 다른 제안 사항이 있으면 알려주십시오. – codemonkey