1

I am trying to insert DbGeography datatype in database via ado.net but I am getting error. Error that I get is:

Additional information: No mapping exists from object type System.Data.Entity.Spatial.DbGeography to a known managed provider native type.

Code that I use: Entity:

public class Position{...
public DbGeography Coordinates { get; set; }
...

DB update:

position.Coordinates = DbGeography.FromText(string.Format("POINT({0} {1})", _longitude, _latitude));
...
cmd.CommandText = @"UPDATE Positions SET [Coordinates] = @Coordinates WHERE PositionId = 1";
cmd.Parameters.Add(new SqlParameter("@Coordinates", store.Coordinates));

What is the way to insert DbGeography type?

1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

2

It will likely be one of two things. I haven't tried directly sending DbGeography to SQL through ADO.NET but if you can then you need to set additional properties on the SqlParameter object, as so:

SqlParameter p = new SqlParameter();
p.Name = "@Coordinates";
p.Value = store.Coordinates;
p.SqlDbType = SqlDbType.Udt;
p.UdtTypeName = "geography";

If that still doesn't work, then you need to convert the DbGeography instance to SqlGeography. For help with that, see my previous SO answer here.

Community
  • 1
  • 1
Jon Bellamy
  • 3,333
  • 20
  • 23