.Net Postgresql Storing Polygon in the Table

Viewed 32

I have a problem with storing polygon in postgresql db through efcore. Whatever I tried, I couldn't able to make it so far and I also searched for a lot of different topic online.

Let me explain everything with code. FYI, I am using Mediator

Here is my DbSet

public class GeoDetail
{
    [Key]
    public int Id { get; set; }

    [Column(TypeName = "geometry (polygon)")]
    public Polygon AreaBoundaries { get; set; }
}

Here is my controller

    [Authorize(PolicyExtensionConstants.RequireOnlyAdmin)]
    [HttpPost]
    public async Task<IActionResult> AddGeoDetail(AddGeoDetailCommand addGeoDetailCommand)
    {
        var result = await _mediator.Send(addGeoDetailCommand);
        return CreateActionResultInstance(result);
    }

Here is the AddGeoDetailCommand object

public class AddGeoDetailCommand : IRequest<Response<AddGeoDetailResponseDto>>
{
    public IEnumerable<AddGeoDetailRequestDto> AreaBoundaries { get; set; }
}

public class AddGeoDetailRequestDto
{
    public double X { get; set; }
    public double Y { get; set; }
}

Here is where I handle saving


    public async Task<Response<AddGeoDetailResponseDto>> Handle(AddGeoDetailCommand request, CancellationToken cancellationToken)
    {
        var company = await _applicationDbContext.Company.Where(x => x.Id == _simpleIdentityAccessors.GetAssignedCompanyId).FirstOrDefaultAsync();

        var mappedRequest = _mapper.Map<GeoDetail>(request); // *** project blows up right in here.
        mappedRequest.Company = company;
        await _applicationDbContext.GeoDetail.AddAsync(mappedRequest);

        company.GeoDetails.Append(mappedRequest);

        await _applicationDbContext.SaveChangesAsync();

        var mappedResponse = _mapper.Map<AddGeoDetailResponseDto>(mappedRequest);

        return Response<AddGeoDetailResponseDto>.Success(mappedResponse);
    }

And here is the error message that I get enter image description here

The project blows up when I try to map AddGeoDetailRequestDto to Polygon object. Since I couldn't figured out how to save polygon into postgresql, I have tried a lot different stuff.

FYI, I also setup the mapping in my AutoMapper class so that should be okay but I am not sure if this is just a mapping issue. But for sake of the solving the problem I will also share my Automapper in here too.

public class AutoMapper : Profile
{

    public AutoMapper()
    {
        CreateMap<GeoDetail, AddGeoDetailResponseDto>().ReverseMap();
        CreateMap<AddGeoDetailRequestDto, Polygon>().ReverseMap();
    }

}

My question is, what am I doing wrong in here, and what is the best way of saving Polygon into the PostgreSql db.

0 Answers
Related