Doctor Ford
Doctor Ford

Reputation: 578

Query SQL table using Entity Framework

I am new to Entity Framework, and I am trying to query one of my tables, and only return the data from one column. I have used this code:

using (var ctx = new TestEntities())
{
    var Pokemon = ctx.C__PokemonName
                     .SqlQuery("select pokemon from __pokemonname")
                     .ToList();

    foreach (var pokemonname in Pokemon)
        MessageBox.Show(Convert.ToString(pokemonname));
}

This code throws an error:

System.Data.Entity.Core.EntityCommandExecutionException
HResult=0x8013193C
Message=The data reader is incompatible with the specified 'TestModel.C__PokemonName'. A member of the type, 'ID', 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, Dictionary2 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, Dictionary2 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__DisplayClass69
1.b__68()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass69
1.b__67()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 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__DisplayClass11.b__10()
at System.Data.Entity.Internal.LazyEnumerator1.MoveNext()
at System.Collections.Generic.List
1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)
at PokeForm.Form1..ctor() in F:\VS Projects\PokeForm\PokeForm\Form1.cs:line 28
at PokeForm.Program.Main() in F:\VS Projects\PokeForm\PokeForm\Program.cs:line 19

Upvotes: 2

Views: 426

Answers (1)

Risto M
Risto M

Reputation: 3009

Easiest way to select * (select all columns) is following:

using (var ctx = new TestEntities())
{
    var pokemonList = ctx.C__PokemonName.ToList();

    foreach (var pokemon in pokemonList)
        MessageBox.Show(Convert.ToString(pokemon.pokemonname));
}        

If you want to query only for single column, you should use i.e Dapper-library with custom sql (select pokemonname from __pokemonname) or you can select one column with Linq .Select-operation and projected anonymous type.

using (var ctx = new TestEntities())
{
    var pokemonNameList = ctx.C__PokemonName.Select(r => new { pokemonname = r.pokemonname }).ToList();

    foreach (var name in pokemonNameList)
        MessageBox.Show(Convert.ToString(name.pokemonname));
}        

Upvotes: 1

Related Questions