Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private static string GetBinaryImportCommand(IReadOnlyList<ColumnMetadata> prope
{
BatchSize = 50_000,
Converters = [PostgreSqlGeometryConverter.Instance],
TypeProviders = [PostgreSqlGeometryConverter.Instance],
};

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using Microsoft.EntityFrameworkCore.Metadata;

using NetTopologySuite.Geometries;

using NpgsqlTypes;

using PhenX.EntityFrameworkCore.BulkInsert.Abstractions;
using PhenX.EntityFrameworkCore.BulkInsert.Options;

namespace PhenX.EntityFrameworkCore.BulkInsert.PostgreSql;

internal sealed class PostgreSqlGeometryConverter : IBulkValueConverter, IPostgresTypeProvider
internal sealed class PostgreSqlGeometryConverter : IBulkValueConverter
{
public static readonly PostgreSqlGeometryConverter Instance = new();

Expand All @@ -27,23 +23,11 @@ public bool TryConvertValue(object source, BulkInsertOptions options, out object
geometry.SRID = options.SRID;
}

result = geometry.ToBinary();
result = geometry;
return true;
}

result = source;
return false;
}

public bool TryGetType(IProperty property, out NpgsqlDbType result)
{
if (property.ClrType.IsAssignableTo(typeof(Geometry)))
{
result = NpgsqlDbType.Bytea;
return true;
}

result = default;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using FluentAssertions;

using Microsoft.EntityFrameworkCore;

using NetTopologySuite.Geometries;

using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
Expand Down Expand Up @@ -71,4 +73,29 @@ public async Task InsertEntities_WithGeo_And_Default_SRID(InsertStrategy strateg
insertedEntities.Should().BeEquivalentTo(entities,
o => o.RespectingRuntimeTypes().Excluding((TestEntityWithGeo e) => e.Id));
}

[SkippableTheory]
[CombinatorialData]
public async Task InsertEntities_WithGeo_And_Search(InsertStrategy strategy)
{
// Arrange
var runId = Guid.NewGuid();

var geo1 = new Point(1, 2) { SRID = 4326 };
var geo2 = new Point(3, 4) { SRID = 4326 };

var entities = new List<TestEntityWithGeo>
{
new TestEntityWithGeo { TestRun = runId, GeoObject = geo1 },
new TestEntityWithGeo { TestRun = runId, GeoObject = geo2 }
};

// Act
await _context.InsertWithStrategyAsync(strategy, entities);

var found = await _context.TestEntitiesWithGeo.Where(x => x.TestRun == runId && x.GeoObject.Distance(geo1) < 1).ToListAsync();

// Assert
Assert.NotEmpty(found);
}
}