Why the Entity Framework binds the same field of Geography type with two different types (SqlGeography and DbGeography) based on way of retrieving data?
I recieve an exception when trying to read data by ExecuteReader
The specified cast from a materialized 'Microsoft.SqlServer.Types.SqlGeography' type to the 'System.Data.Entity.Spatial.DbGeography' type is not valid.
Here is a table:
CREATE TABLE [OneGeoField]([Position] [geography] NOT NULL
And data:
INSERT INTO OneGeoField (Position) VALUES(geography::STGeomFromText('POINT (30.5047009495918 59.862826902743)',4326))
EF generates a class for this table with DbGeography type of Position field:
public partial class OneGeoField
{
public System.Data.Entity.Spatial.DbGeography Position { get; set; }
}
When retrieving data with SqlQuery method it works fine
var query = "SELECT * FROM OneGeoField";
var list = db.Database.SqlQuery<OneGeoField>(query).ToList();
But in case of using ExecuteReader, it throws an exception
try
{
var cmd = db.Database.Connection.CreateCommand();
db.Database.Connection.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
var objectContext = ((IObjectContextAdapter)db).ObjectContext;
var list = objectContext.Translate<Core.OneGeoField>(reader).ToList();
}
catch (Exception ex)
{
throw;
}
finally
{
db.Database.Connection.Close();
}
Is it only solution to have a different model for ExecuteReader, and another for common approach?