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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.