Closed yahorsi closed 2 years ago
@yahorsi If your database has geometry
types (as opposed to the more common geography
types) then you need to tell EF this in the mapping. See https://docs.microsoft.com/en-us/ef/core/modeling/spatial#sql-server
@yahorsi If your database has
geometry
types (as opposed to the more commongeography
types) then you need to tell EF this in the mapping. See https://docs.microsoft.com/en-us/ef/core/modeling/spatial#sql-server
It might be that I didn't understand something, but the code I have posted and created repository for works in for the 2.2 and breaks in 3.0 and higher? We're using Point type, not sure if it is geometry or geography, in the Sql Server Database database it is declared as [geometry]
Are there any breaking changes in the spatial types support?
@yahorsi It's possible this worked for 2.x because we changed slightly the way parameters are sent to the database in 3.0. It worked in 2.x by accident.
/cc @bricelam
@yahorsi It's possible this worked for 2.x because we changed slightly the way parameters are sent to the database in 3.0. It worked in 2.x by accident.
/cc @bricelam
In the article you provided there is no any sample on how to "you need to tell EF this in the mapping". Poin class in the article is used without any annotations.
[Table("Cities", Schema = "Application"))]
class City
{
public int CityID { get; set; }
public string CityName { get; set; }
public Point Location { get; set; }
}
PS If it was working just by mistake and it does not now it is still breaking change so it at least worth documenting
@yahorsi From the article:
Geography or geometry By default, spatial properties are mapped to geography columns in SQL Server. To use geometry, configure the column type in your model.
Agreed that this is a breaking change.
@yahorsi From the article:
I just meant in the article there is no sample on how to do that
@yahorsi If your database has
geometry
types (as opposed to the more commongeography
types) then you need to tell EF this in the mapping. See https://docs.microsoft.com/en-us/ef/core/modeling/spatial#sql-server
Just to clarify, do you mean we should use something like?
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<FooTable>(entity =>
{
entity.Property(e => e.Location).HasColumnType("geometry");
});
}
@yahorsi Yes, that or use ColumnAttribute
.
Support for Spatial data in Microsoft Sql Server is partially broken in all versions starting from 2.2.6. Currently EF in version starting from 3.0 is just unable to add table records containing spatial column (happens if spatial column is not null).
Here is project to demo the problem. https://github.com/yahorsi/Ef31SqlServerSpatialBug Projects contains both database to create the database and .NET code to demo the bug. Works just fine if you use 2.2.6 and failing in 3.0, 3.1, 3.1.1
Steps to reproduce
Create Microsoft Sql Database database using following script:
And corresponding C# code
Exception
Further technical details
EF Core version: 3.0, 3.1, 3.1.1 Database provider: Microsoft Sql Server Target framework: 3.1 Operating system: Windows 10 IDE: Visual Studio 2019 16.4.2