I'm trying to insert a Geometry object through an Entity class in my Database, but the error "geography is incompatible with geometry" always returns. I initially receive a string passing the X and Y values of what would be a geometrical coordinate and do the necessary conversions to create the Point type that Geometry offers, I don't need to make any calculations so Geometry type is the best for me to use right now. I use the function below to do the convertion:
public static Geometry ConvertToGeometry(string coordinatesAux)
{
if(coordinatesAux == null)
throw new NullReferenceException();
NumberFormatInfo formatProvider = new NumberFormatInfo();
formatProvider.NumberGroupSeparator = ".";
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
var currentLocation = geometryFactory.CreatePoint(new Coordinate(
Convert.ToDouble(coordinatesAux.Substring(0, coordinatesAux.IndexOf(",")), formatProvider),
Convert.ToDouble(coordinatesAux.Substring(coordinatesAux.IndexOf(",") + 1), formatProvider)));
return currentLocation;
}
I also made the necessary modification in Startup.cs for db to use NetTopologySuite, but when I try to insert the value through the API, it returns the error described initially. I don't know if the problem is in the way I did to convert from string to Geometry, or if it is the Entity itself that recognizes the field as a Geography Point.
The value I try to pass is -20.338113, -40.287893 and the Entity class I use:
public class Address : EntityBase<int>
{
public Address(string district, string street, int number, string complement, string zipCode,
string cityDescription, string stateDescription, Geometry coordinates, int countryId, byte stateId, int cityId)
{
District = district;
Street = street;
Number = number;
Complement = complement;
ZipCode = zipCode;
CityDescription = cityDescription;
StateDescription = stateDescription;
Coordinates = coordinates;
CountryId = countryId;
StateId = stateId;
CityId = cityId;
}
public string District { get; set; }
public string Street { get; set; }
public int Number { get; set; }
public string Complement { get; set; }
public string ZipCode { get; set; }
public string CityDescription { get; set; }
public string StateDescription { get; set; }
public virtual Geometry Coordinates { get; set; }
public int CountryId { get; set; }
public byte StateId { get; set; }
public int CityId { get; set; }
public int? CustomerId { get; set; }
public int? StoreId { get; set; }
public int? ProfessionalId { get; set; }
}
So, asking again, is something that Entity framework do or I made a mistake? If more parts of the code are needed, I will edit the question to add.
Edit 1
Table Address:
CREATE TABLE Address(
Id INT PRIMARY KEY IDENTITY(1, 1),
District VARCHAR(50) NOT NULL, -- Bairro
Street VARCHAR(100) NOT NULL, -- Rua
--Description VARCHAR(100) NOT NULL,
Number INT,
Complement VARCHAR(100),
ZipCode VARCHAR(20) NOT NULL,
CityDescription VARCHAR(100),
StateDescription VARCHAR(100),
Coordinates GEOMETRY,
CountryId INT FOREIGN KEY REFERENCES Country(Id) NOT NULL,
StateId TINYINT FOREIGN KEY REFERENCES State(Id),
CityId INT FOREIGN KEY REFERENCES City(Id),
CustomerId INT FOREIGN KEY REFERENCES Customer(Id),
StoreId INT FOREIGN KEY REFERENCES Store(Id),
ProfessionalId INT FOREIGN KEY REFERENCES Professional(Id),
INDEX IndexAddressCountryId NONCLUSTERED (CountryId),
INDEX IndexAddressStateId NONCLUSTERED (StateId),
INDEX IndexAddressCityId NONCLUSTERED (CityId),
INDEX IndexAddressCustomerId NONCLUSTERED (CustomerId),
INDEX IndexAddressStoreId NONCLUSTERED (StoreId),
INDEX IndexAddressProfessionalId NONCLUSTERED (ProfessionalId)
)