简体   繁体   English

更新父实体不会更新子实体

[英]Updating Parent Entity doesn't Update Child Entity

Whether attaching parent entity to the context again and updating it should update the child entity? 是否将父实体再次附加到上下文并更新它应该更新子实体? Am i missing something? 我错过了什么吗?

Or should I necessarily write the updating EF logic (in DAL) for the child entity? 或者我是否必须为子实体编写更新EF逻辑(在DAL中)?

This is my sample code model: 这是我的示例代码模型:

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() code: _objParent.Update()代码:

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

UPDATE (Parent Loading Code Sample) 更新(父代加载代码示例)

    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()
        }
    }

EF only track changes from the point where it gets to know about the object. EF仅跟踪对象的了解。 The most simple way to work with updates in EF is to load the existing object from the DB, update it and then save it back. 在EF中使用更新的最简单方法是从数据库加载现有对象,更新它然后将其保存回来。 In this case it means that you should load the ParentEntity object from the DB. 在这种情况下,它意味着您应该从DB加载ParentEntity对象。

The other approach is to use Attach as you do. 另一种方法是像你一样使用Attach In that case you should first call attach the unchanged ParentEntity and then call ParentEntity.ChildEntity.Add(entityChild) . 在这种情况下,您应首先调用附加未更改的 ParentEntity ,然后调用ParentEntity.ChildEntity.Add(entityChild)

Another alternative is to explicitly add the new ChildEntity directly to the DbContext, then you could simply set the foreign key value of the ChildEntity to the key value of the ParentEntity to make them connected. 另一种选择是明确地添加新的ChildEntity直接到的DbContext,那么你可以在的外键值简单的设置ChildEntity到的键值ParentEntity使它们连接。

You should load and update your entities inside the using statement - this way all the changes will be tracked by the Entity Framework: 您应该在using语句中加载和更新实体 - 这样,实体框架将跟踪所有更改:

using (var context = new DBEntities())
{
    // load and update entities
    // ....

    context.SaveChanges();
}

UPDATE - This is just an example, I'll keep it simple. 更新 - 这只是一个例子,我会保持简单。 I would create a service where I'd put my logic - something like this: 我会创建一个服务,我会把我的逻辑 - 这样的东西:

public class ParentService
{
    // other service methods here

    protected DBEntities CreateContext()
    {
        return new DBEntities();
    }

    public ParentEntity Update(int id, string columnA, string columnB)
    {
        ParentEntity _entity = null;

        using (var context = CreateContext())
        {
            bool isNew = false;

            _entity = context.ParentEntity
                .Include("ChildEntity")
                .SingleOrDefault(e => e.parent_id == id);

            ChildEntity entityChild = ParentEntity.ChildEntity.SingleOrDefault();

            if(entityChild == null)
            {
                entityChild = new ChildEntity();
                isNew = true;
            }

            entityChild.ColumnA = columnA;
            entityChild.ColumnB = columnB;

            if(isNew)
            {
                ParentEntity.ChildEntity.Add(entityChild);
            }

            context.SaveChanges();
        }

        return _entity;
    }
}

In the UI code: 在UI代码中:

string id = .....;
string columnA= txtA.Text;
strign columnB = txtB.Text;

var service = new ParentService();
ParentEntity parent = service.Update(id, columnA, columnB);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试更新父实体时,实体框架正在更新子实体 - Entity Framework is updating child entity when I try to update parent entity 在 EF 中更新父实体时更新子实体 - update child entities when updating a parent entity in EF 如何在 EF 中更新父实体时添加/更新子实体 - How to add/update child entities when updating a parent entity in EF 为什么先更新EF代码中的父实体和子实体时,为什么不必更新父对象 - Why don't I have to update a parent object when updating both the parent and child entity in EF code first 在 Entity Framework Core 中更新父实体时添加或更新子实体 - Add or Update child entities when updating a parent entity in Entity Framework Core 如果不通过实体框架中的父代,则无法更新子实体 - Can't update child entity without going through parent in entity framework 从父实体更新子实体 - Update child entities from parent entity 一次添加新的子实体并更新父实体 - Add new child entity and update parent entity in one turn 当微风子实体更新父实体状态不变时 - when breeze child entity update parent entity state is not change 实体框架 6.1:在创建父实体时更新子 ICollection - Entity Framework 6.1: update child ICollection when the parent entity is created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM