Skip to content

Materializing results from a SqlDataReader (Translate from EF6.x) #4675

Description

@jnm2

To avoid round trips, a large number of small queries (sometimes up to 40) are batched into a single command. Most of the result sets of these queries are outside the scope of Entity Framework, but some result sets are intended to be used by Entity Framework to materialize entities.

The use case is exactly the same as RawSql but batched with other queries, only some of which should be handled by Entity Framework.

In EF6, we had ObjectContext.Translate. Can we have such an API on EF Core as well in the future?

In the meantime, where is the extensibility point in EntityFramework.MicrosoftSqlServer or EntityFramework.Relational that deals with DbDataReader? Specifically I want to simulate Set<T>().AsNoTracking().RawSql(q) but hand in my own SqlDataReader.

Activity

  1. changed the title [-]Materializing results from a SqlDataReader[/-] [+]Materializing results from a SqlDataReader (Translate from EF6.x)[/+] on Mar 4, 2016
  2. added this to the Backlog milestone on Mar 4, 2016
  3. rowanmiller commented on Mar 4, 2016

    @rowanmiller
    Contributor

    Agreed that a top level API would be good - we'll look at this post 1.0.0

  4. rowanmiller commented on Mar 4, 2016

    @rowanmiller
    Contributor

    IRelationalValueBufferFactory is probably the low-level piece you are after

  5. jnm2 commented on Mar 8, 2016

    @jnm2
    Author

    I think I finally got my head wrapped around what stuff does. Until you have API support for FromReader, here's my workaround. It does not handle inheritance or tracking, since I don't need or want those at the moment for this use case.

    Usage:

    var rows = await dbContext.TestObjects.FromReaderAsync(dbDataReader);

    Code:

    public static class DbSetExtensions
    {
        public static async Task<IReadOnlyList<T>> FromReaderAsync<T>(this DbSet<T> set, DbDataReader reader) where T : class
        {
            var valueBufferParameter = Expression.Parameter(typeof(ValueBuffer));
            var materializer = Expression.Lambda<Func<ValueBuffer, T>>(
                set.GetService<IEntityMaterializerSource>().CreateMaterializeExpression(
                    set.GetService<IModel>().FindEntityType(typeof(T)),
                    valueBufferParameter),
                valueBufferParameter).Compile();
    
            var valueBufferFactory = set.GetService<IRelationalValueBufferFactoryFactory>().Create(new[] { typeof(T) }, null);
    
    
            var r = new List<T>();
    
            while (await reader.ReadAsync())
                r.Add(materializer.Invoke(valueBufferFactory.Create(reader)));
    
            return r.AsReadOnly();
        }
    }

    Bit better factored here for both FromReader and FromReaderAsync.

  6. ajayvikas commented on Aug 28, 2017

    @ajayvikas

    We want to port our code from EF6 to EF core. However we have been using Translate extensively because of the performance reason. availability of this feature is absolutely must for us.

  7. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions