私は最初に EF コードを使用しており、EF 4.X DbContext Fluent Generator T4 でコードを生成しているため、2 つの poco エンティティがあります ( BindingList<T>
winForms でバインディングを使用するようにリストを変更しました)。
public partial class Parent
{
public Parent()
{
this.Childs = new BindingList<Childs>();
}
int _ParentId;
public int ParentId { get; set; }
BindingList<Child> _Childs;
public virtual BindingList<Child> Childs { get; set; }
}
public partial class Child
{
int _ChildId;
public int ChildId { get; set; }
int _ParentId;
public int ParentId { get; set; }
Parent_Parent;
public virtual Parent Parent { get; set; }
}
また、私のマッピングファイルは次のとおりです。
public Parent_Mapping()
{
this.HasKey(t => t.ParentId);
this.ToTable("Parent");
this.Property(t => t.ParentId).HasColumnName("ParentId");
}
public Child_Mapping()
{
this.HasKey(t => t.ChildId);
this.ToTable("Child");
this.Property(t => t.ChildId).HasColumnName("ChildId");
this.Property(t => t.ParentId).HasColumnName("ParentId").IsRequired();
this.HasRequired(t => t.Parent)
.WithMany(t => t.Childs)
.HasForeignKey(t=>t.ParentId)
.WillCascadeOnDelete(true);
}
私のDbContextには、これらのコードがあります:
public partial class MyContext : DBContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new Parent_Mapping());
modelBuilder.Configurations.Add(new Child_Mapping());
}
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Childs { get; set; }
}
そのため、カスケード削除を有効にした1対多の関係があります。
しかし、親エンティティを削除したい場合、次のエラーが発生しました:
{
"Cannot insert the value NULL into column 'ParentId', table 'MyDB.dbo.Child';
column does not allow nulls. UPDATE fails.\r\nThe statement has been terminated."
}
監視にEFプロファイラーを使用したとき、EFが子テーブルを更新してParentIdをNullに設定し、代わりに親エンティティを削除しようとしていることがわかりました!:
update [dbo].[Child]
set
[ParentId] = null,
where ([ChildId] = 2 /* @1 */)
私の間違いはどこですか?