Rize S. answered 03/23/23
Master's in MISM, 25 yrs Exp: SQL Specialist
You can use the EF or Linq2Sql to insert and query the geography data. Here is an example of inserting geographic data into SQL Server with C# and EF.
First, create a class for the geography data:
public class GeographyData
{
public int Id { get; set; }
public DbGeography Polygon { get; set; }
public DbGeography Point { get; set; }
}
Then, create the database context to hold the geography data.
public class GeographyContext : DbContext
{
public DbSet<GeographyData> GeographyData { get; set; }
}
Next, create a method to insert the geography data into the database.
public static void InsertGeographyData()
{
using (var context = new GeographyContext())
{
var polygon = DbGeography.FromText("POLYGON((1 1,2 2,3 3,4 4))");
var point = DbGeography.FromText("POINT(5 5)");
var data = new GeographyData { Polygon = polygon, Point = point };
context.GeographyData.Add(data);
context.SaveChanges();
}
}
Finally, query the geography data from the database.
public static void QueryGeographyData()
{
using (var context = new GeographyContext())
{
var data = context.GeographyData.FirstOrDefault();
Console.WriteLine(data.Polygon);
Console.WriteLine(data.Point);
}
}
You can find more information about using EF and Linq2Sql with geography data here: https://docs.microsoft.com/en-us/ef/ef6/modeling/spatial/