-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support spatial types in JSON columns #28811
Comments
This exception is about floating point numbers. Given this is exception, we should just enable the option in serializer we use. (After fixing this, the spatial type may run into other errors) |
@smitpatel I think it's a bogus exception message. I'll create a full repro so that you can investigate more if you wish. |
Could be some random component of spatial type showing inf value and not serializing properly. I believe the same exception could also be hit with a double property and trying to save infinity (though not sure what is current behavior outside of JSON for that.) |
Below is a full repro. As suspected, the issue isn't really about number handling, but that the Fundamentally, I think type mappings need to have a way to configure the JSON serialization used. I think that's likely best tracked by another issue, but I'd like to keep this one specifically about spatial support. public static class Your
{
public static string ConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow";
}
public class Blog
{
public int Id { get; set; }
public Location Location { get; set; }
}
public class Location
{
public Point Point { get; set; }
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.ConnectionString, b => b.UseNetTopologySuite())
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
public DbSet<Blog> Blogs => Set<Blog>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().OwnsOne(
e => e.Location, b =>
{
b.ToJson();
});
}
}
public class Program
{
public static void Main()
{
var point = new Point(115.7930, 37.2431) { SRID = 4326 };
var options = new JsonSerializerOptions(JsonSerializerDefaults.General);
options.NumberHandling |= JsonNumberHandling.AllowNamedFloatingPointLiterals;
options.ReferenceHandler = ReferenceHandler.Preserve;
var json = JsonSerializer.Serialize(point, options);
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Blog { Location = new Location { Point = new Point(115.7930, 37.2431) { SRID = 4326 } } });
context.SaveChanges();
}
}
}
|
/cc @bricelam |
Depends on #28835. |
This is a very similar problem to #17317 where we also want to inject the GeoJSON representation into the parent. |
This will be fixed for .NET 8? I will go back to Newtonsoft untill this is fixed. Thanks! |
I was getting this error too, turns out I didn't configure my serializer completely.
Adding the GeoJsonConverterFactory was the key. Its in the package NetTopologySuite.IO.GeoJSON4STJ for those using System.Text.Json serializer and not the newtonsoft serializer. There is another package for newtonsoft users. Hope this helps someone out! |
📝 Design Meeting NoteSQL Server doesn't support GeoJSON. It would be more appropriate to embed the WKT (or WKB or GML) into the JSON column there so you can perform spatial operations in SQL, and interop with regular spatial columns. |
Current exception:
The text was updated successfully, but these errors were encountered: