EF: Include navigation property to result of adding entity in DbSet

Viewed 1732

I am adding an entity to my DataContext. After this, I have to send this entity back on view (show result to end user).

DbSet has Add method that returns back new entity. But I need my entity with included navigation properties.

In the current moment, I make one more call to DataContext and passing newEntity's Id to find the entity.

public async Task<Entity> AddAsync(Entity entity)
{
    var savedEntity = m_DataContext.Entities.Add(entity);
    await m_DataContext.SaveChangesAsync();
    return await m_DataContext.Entities
        .Include(p=>p.SubEntity)
        .FirstAsync(p=>p.Id==savedEntity.Id);
}

Is it possible to include navigation property during adding operation?

My code works but I am looking for way to do it more gracefully.

2 Answers
Related