3

親エンティティを再度コンテキストにアタッチして更新すると、子エンティティが更新されるかどうか。何か不足していますか?

または、子エンティティの更新 EF ロジックを (DAL で) 必ず記述する必要がありますか?

これは私のサンプルコードモデルです:

ChildEntity entityChild;
if (ParentEntity.ChildEntity.SingleOrDefault() != null)
    entityChild = ParentEntity.ChildEntity.SingleOrDefault();
else
{
    entityChild = new ChildEntity();
    ParentEntity.ChildEntity.Add(entityChild);
}

entityChild.ColumnA= txtA.Text;
entityChild.ColumnB= txtB.Text;

// Send entityParent for update
_objParent.Update(entityParent)

_objParent.Update() コード:

context.vouchers.Attach(entityParent);
ObjectStateEntry objectState = context.ObjectStateManager.GetObjectStateEntry(entityParent);
objectState.ChangeState(System.Data.EntityState.Modified);
context.SaveChanges();

UPDATE (親ローディングのコード サンプル)

    public ParentEntity GetById(int id)
    {
        using (var context = new DBEntities())
        {
            ParentEntity _entity = context.ParentEntity
                .Include("ChildEntity")
                .Where(e => e.parent_id == id);

            return (ParentEntity)_entity.SingleOrDefault()
        }
    }
4

2 に答える 2