4

I have a parent table (from oracle dataBase) and some child tables, and I can successfully update the parent entity using the Update method below, but not the child ones.

By the way, can I use with generic entities here?

public void UpdateEnt<T>(T entity) where T : parent_table
{
    if (entity == null)
    {
        throw new ArgumentException("Cannot add a null entity.");
    }

    using (var _context = (DataModelDetach)d.GetContextForRead(Module))
    {
        var entry = _context.Entry<T>(entity);

        if (entry.State == EntityState.Detached)
        {
            var set = _context.Set<T>();
            T attachedEntity = set.Include(x => x.children_1).Include(x => x.children_2).SingleOrDefault(e => e.idSeq == entity.idSeq);  

            if (attachedEntity != null)
            {
                var attachedEntry = _context.Entry(attachedEntity);
                attachedEntry.CurrentValues.SetValues(entity);
            }
            else
            {
                entry.State = EntityState.Modified; // This should attach entity
            }
        }
    }
}
user1012506
  • 2,048
  • 1
  • 26
  • 45

0 Answers0