efcore: Generic FromSQL() method does not work when we configure a column to map with a different property name

Message = “The required column ‘BlogId’ was not present in the results of a ‘FromSql’ operation.”

I have a generic method ExecuteStoreQuery method which looks like following:

public override IEnumerable<TEntity> ExecuteStoreQuery<TEntity>(String commandText, params object [] parameters)
{
            return _context.Set<TEntity>().FromSql(query, parameters);
            
}

The model looks like following:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

and the mappings look like following:

modelBuilder.Entity<Blog>().ToTable("Blog");
            modelBuilder.Entity<Blog>().Property(t => t.blog_id).HasColumnName("BlogId");
            modelBuilder.Entity<Blog>().Property(t => t.Url ).HasColumnName("Url");

and whenever I call the ExecuteStoreQuery Method, I get a exception.

                queryResult = repository.ExecuteStoreQuery<Blog>("SELECT blogid as blog_id, url from blog", parameters).ToList();
Exception message:
The required column 'BlogId' was not present in the results of a 'FromSql' operation.
   at Microsoft.EntityFrameworkCore.Query.Sql.Internal.FromSqlNonComposedQuerySqlGenerator.CreateValueBufferFactory(IRelationalValueBufferFactoryFactory relationalValueBufferFactoryFactory, DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.<NotifyReaderCreated>b__11_0(FactoryAndReader s)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(Boolean buffer)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

Further technical details

EF Core version: 2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 7 IDE: Visual Studio 2017

Please let me know how if there is work around of this issue.

The EF6 function however works ((System.Data.Entity.Infrastructure.IObjectContextAdapter)_context).ObjectContext.ExecuteStoreQuery<TEntity>(query, parameters);

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@swastiks There is some background on EF6 SqlQuery column mapping in this issue: http://entityframework.codeplex.com/workitem/233 Might help explain some of what you are seeing on EF6.

Hi @swastiks, Sorry, I do not know why. Nor can I simply say that it is a failure. My answers are limited to EF Core. If you have any questions regarding EF Core we can clarify.

More certainty, someone on the team will give you a more adequate answer.

c/ @ajcvickers @smitpatel

@swastiks - already did. We will investigate the project you shared and let you know findings. Thanks.