Generic FromSQL() method does not work when we configure a column to map with a different property name
See original GitHub issueMessage = “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);
Issue Analytics
- State:
- Created 6 years ago
- Comments:15 (9 by maintainers)
Top Results From Across the Web
EF Core FromSql for same model but with different queries ...
But the problem I am facing, is that there are stored procedures that I cannot change. And these stored procedures does not return...
Read more >Executing Raw SQL Queries using FromSql Method
The FromSql method in Entity Framework Core is used to execute raw SQL queries against the database and return the results as entities....
Read more >Calling Stored Procedures with the Entity Framework in . ...
This method returns a false when there are no more records in the first result set. Each time through the loop, create a...
Read more >Executing Raw SQL Queries using Entity Framework Core 6
The mapping process depends on the class property name, data type vs column name, and data type. This auto mapping is done using...
Read more >SQL Queries - EF Core
The column names in the result set must match the column names that properties are mapped to. Note that this behavior is different...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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