添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

expected type was 'NetTopologySuite.Geometries.Point' but the actual type was 'GeoJSON.Net.Geometry.Point'

Ask Question _searchService.DeleteIndicies(SearchService.SearchIndexMappings[typeof(VeganItemEstablishmentSearchDto)]); _searchService.CreateIdx<VeganItemEstablishmentSearchDto>(); var allVeganItemEstablishmentsArray = allVeganItemEstablishments.ToArray(); if (allVeganItemEstablishmentsArray.Any()) var allItems = allVeganItemEstablishments/*.Include(r => r.Establishment)*/.ToArray(); var searchResults = allItems.Select(item => { var toReturn = _mapper.Map<VeganItemEstablishmentSearchDto>(item); return toReturn; _searchService.Index(searchResults.ToArray()); return true;

I get:

Exception has occurred: CLR/System.InvalidOperationException An unhandled exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll: 'An error occurred while reading a database value for property 'Establishment.Location'. The expected type was 'NetTopologySuite.Geometries.Point' but the actual value was of type 'GeoJSON.Net.Geometry.Point'.' Inner exceptions found, see $exception in variables window for more details. Innermost exception System.InvalidCastException : Can't cast database type public.geometry to Point at Npgsql.Internal.TypeHandling.NpgsqlTypeHandler.ReadCustom[TAny](NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription)
Npgsql.Internal.TypeHandling.NpgsqlTypeHandler.Read[TAny](NpgsqlReadBuffer buf, Int32 len, FieldDescription fieldDescription) at Npgsql.NpgsqlDataReader.GetFieldValue[T](Int32 ordinal)

The model:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using NetTopologySuite.Geometries;
namespace Vepo.Domain
    [Serializable]
    public class Establishment : CreatedBySomeone
        [Required]
        public string Name { get; set; }
        [Required]
        public string PlaceId { get; set; }
        [Required]
        public string Street { get; set; }
        public string Suburb { get; set; }
        public string City { get; set; }
        public string StreetNumber { get; set; }
        [Column(TypeName="geometry (point)")]
        public Point Location { get; set; }

The db insertion code:

        if (entity.Establishment != null) {
            establishments.AddIfNotExists<Establishment>(
                entity.Establishment, 
                x => x.PlaceId == entity.Establishment.PlaceId);
            await context.SaveChangesAsync();
            establishmentId = establishments.Single(a => a.PlaceId == entity.Establishment.PlaceId).Id;
            toReturnEstablishment.Id = (int)establishmentId;
    public static EntityEntry<T> AddIfNotExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>> predicate = null) where T : class
        var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any();
        return exists ? null : dbSet.Add(entity);

OnModelCreating:

    modelBuilder.Entity<Establishment>(establishment =>
        establishment.HasIndex("PlaceId").IsUnique();
        establishment.Property(u => u.CreatedDate)
            .HasDefaultValueSql("CURRENT_TIMESTAMP"); 
        establishment.Property(u => u.UpdatedDate)
            .HasDefaultValueSql("CURRENT_TIMESTAMP");
        establishment.HasOne(q => q.UpdatedBy)
            .WithMany()
            .HasForeignKey(k => k.UpdatedById);
        establishment.HasOne(q => q.CreatedBy)
            .WithMany()
            .HasForeignKey(k => k.CreatedById);

Any idea why Include(r => r.Establishment) gets type GeoJSON.Net.Geometry.Point?

Yes thanks. I deleted GeoJson and deleted NpgsqlConnection.GlobalTypeMapper.UseGeoJson(); from startup.cs and added NpgsqlConnection.GlobalTypeMapper.UseNetTopologySuite(); in its place, and viola, it works. – BeniaminoBaggins Mar 7, 2022 at 18:02

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.