Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: Queries run through DbSet.SqlQuery do not appear to have mapping applied.

$
0
0
I am fairly sure this is a error on my part or something "working as intended" but I did not realize it is intended that way which is why I am posting it here instead of in Issues. It appears that if you run a query through DbSet<T>.SqlQuery or through Database.SqlQuery<T> it will not apply any custom mapping you have applied against the model.

Here is a SSCCE
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;

namespace EntityFrameworkTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new TestingContext())
            {
                var result1 = context.Foos.ToArray(); //Works

                var result2 = context.Foos.SqlQuery("select * from Foos").ToArray(); //Fails.
                Debugger.Break();
            }
        }
    }

    class TestingContext : DbContext
    {
        static TestingContext()
        {
            Database.SetInitializer<TestingContext>(new Initializer());
        }

        public DbSet<Foo> Foos { get; set; }
    }

    class Initializer : DropCreateDatabaseAlways<TestingContext>
    {
        protected override void Seed(TestingContext context)
        {
            context.Foos.Add(new Foo());
        }
    }

    class Foo
    {
        [Key]
        public int FooId { get; set; }
        [Column("WithDiffrentName")]
        public string Data { get; set; }
    }
}
It fails with the exception
System.Data.Entity.Core.EntityCommandExecutionException was unhandled
  HResult=-2146232004
  Message=The data reader is incompatible with the specified 'EntityFrameworkTest.Foo'. A member of the type, 'Data', does not have a corresponding column in the data reader with the same name.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetMemberOrdinalFromReader(DbDataReader storeDataReader, EdmMember member, EdmType currentType, Dictionary`2 renameList)
       at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetColumnMapsForType(DbDataReader storeDataReader, EdmType edmType, Dictionary`2 renameList)
       at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndType(DbDataReader storeDataReader, EdmType edmType, EntitySet entitySet, Dictionary`2 renameList)
       at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate[TElement](DbDataReader reader, String entitySetName, MergeOption mergeOption, Boolean streaming, EntitySet& entitySet, TypeUsage& edmType)
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
       at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass24`1.<ExecuteStoreQueryReliably>b__23()
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
       at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass24`1.<ExecuteStoreQueryReliably>b__22()
       at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
       at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
       at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassa.<ExecuteSqlQuery>b__9()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at EntityFrameworkTest.Program.Main(String[] args) in d:\Code\EntityFrameworkTest\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Other than doing select FooId, WithDiffrentName as Data from Foos is there a step I am missing to make it work correctly?

Viewing all articles
Browse latest Browse all 1793

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>