0
votes

I am developing an API in the .net core and using the Framework entity.

My bank already existed and in one of the tables I have a Point type field to store coordinates (Spatials).

I'm not using any automatic approach (Ex: code First, DataBase First ...), I am modeling my classes myself.

To map this Point field I did it as in primitive types, I believe to be wrong, and I'm getting an error:

System.InvalidOperationException: 'The property 'Address.LatLong' could not be mapped, because it is of type 'Point' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'

public class Address:BaseModel
    {

        [Required]
        [StringLength(30)]
        public string Street { get; set; }
        [Required]
        public int Number { get; set; }

        [StringLength(45)]
        public string Observation { get; set; }

        [Required]
        [StringLength(20)]
        public string PostalCode { get; set; }

        [ForeignKey(nameof(City))]
        [Required]
        public int CityId { get; set; }
        public virtual City City { get; set; }

        public Point LatLong { get; set; } //this is the field

    }
2

2 Answers

0
votes

Assuming you want to map Point.X and Point.Y to 2 columns in that same table just add 2 properties and then an ignored property for LatLog. Use a getter / setter that sets or returns the values into a new Point. That would be the easiest way about it.

public class Address:BaseModel
{
    /* Other members removed for brevity */

    public int Lat { get; set; }
    public int Long { get; set; }

    [NotMapped]
    public Point LatLong { get { return new Point(Lat, Long); } set { this.Lat = value.X; this.Long = value.Y; } }
}
0
votes

if you wanna use that point for other address , so u should add something like this to your point Class :

public virtual ICollection<Address> Address{ get; set; }

and if you dont wanna use that point for another adress , u can bring Lat and Long to your adress class.

by the way i think add this public virtual ICollection<Address> Address{ get; set; } should fix your problem