dotnet / dotnet/efcore

Seeded geometry is re-updated when scaffolding new migration [NetTopologySuite]

Open
#27,374 3 comments 0 reactions 0 assignees View on GitHub
area-migrations-seeding area-spatial customer-reported poachable
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

## Seeded geometry is re-updated when scaffolding new migration [NetTopologySuite]

Probably a configuration error, but I am not sure how to debug it and could not find any examples on seeding geometry.

The geometry is seeded as expected, but every time we scaffold a new migration the same identical values are re-updated. Would expect to only include these in a single migration unless the values change.

Seems relevant: #18729

Code first model:

```cs
public class ApiDbContext : AppDbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(area =>
{
area.ToTable("geo_areas", DbSchemeName);
area.HasData(GeoAreaSeeder.GetGeoAreas());
});
}
}
```

Geometry seed values:
```cs
public static class GeoAreaSeeder
{
public static GeoArea[] GetGeoAreas()
{
var geometryFactory = CreateGeometryFactory(); // We reuse a lazy, static instance.
return new[]
{
new GeoArea
{
Id = new Guid("00000001-0000-0000-0000-000000000000"),
Name = "Trondheim",
Description = "Seeded.",
Type = GeoAreaType.City,
Polygon = CreateRectanglePolygon(geometryFactory,
new Coordinate(10.0358345802475, 63.2432650994015),
new Coordinate(10.5668781234632, 63.4730293396388))
},
// ...more
};
}

private static GeometryFactory CreateGeometryFactory()
{
// Initialize default spatial data parameters: https://github.com/NetTopologySuite/NetTopologySuite/wiki/GettingStarted
var services = new NtsGeometryServices(
NetTopologySuite.Geometries.Implementation.CoordinateArraySequenceFactory.Instance,
new PrecisionModel(1e4), // 4 decimal places (65.1234).
srid: SridValues.Wgs84_4326, // SRID 4326 refers to WGS 84 coordinate system.
GeometryOverlay.NG, // Use the next-gen overlay.
new CoordinateEqualityComparer()); // Default comparer.

NtsGeometryServices.Instance = services;
return services.CreateGeometryFactory();
}

private static Polygon CreateRectanglePolygon(GeometryFactory geometryFactory, Coordinate min, Coordinate max)
{
// Must be a closed polygon and counter-clockwise.
return geometryFactory.CreatePolygon(new[]
{
min,
new Coordinate(max.X, min.Y),
max,
new Coordinate(min.X, max.Y),
min, // Closed ring
});
}
}

```

1st scaffolded migration:
```cs
migrationBuilder.InsertData(
schema: "api",
table: "geo_areas",
columns: new[] { "Id", "Description", "ImportNote", "ImportedTime", "Name", "Polygon", "Type" },
values: new object[,]
{
{ new Guid("00000001-0000-0000-0000-000000000000"), "Seeded.", null, null, "Trondheim", (NetTopologySuite.Geometries.Polygon)new NetTopologySuite.IO.WKTReader().Read("SRID=4326;POLYGON ((10.03583 63.24327, 10.56688 63.24327, 10.56688 63.47303, 10.03583 63.47303, 10.03583 63.24327))"), "City" },
// ...multiple of these
```

2nd scaffolded migration:

```cs
// ...multiple of these
migrationBuilder.UpdateData(
schema: "api",
table: "geo_areas",
keyColumn: "Id",
keyValue: new Guid("00000001-0000-0000-0000-000000000000"),
column: "Polygon",
value: (NetTopologySuite.Geometries.Polygon)new NetTopologySuite.IO.WKTReader().Read("SRID=4326;POLYGON ((10.03583 63.24327, 10.56688 63.24327, 10.56688 63.47303, 10.03583 63.47303, 10.03583 63.24327))"));
```

1st scaffolded SQL:

```sql
IF NOT EXISTS(SELECT * FROM [api].[_migration_history] WHERE [MigrationId] = N'20211108173632_Add_GeoArea')
BEGIN
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Description', N'ImportNote', N'ImportedTime', N'Name', N'Polygon', N'Type') AND [object_id] = OBJECT_ID(N'[api].[geo_areas]'))
SET IDENTITY_INSERT [api].[geo_areas] ON;
EXEC(N'INSERT INTO [api].[geo_areas] ([Id], [Description], [ImportNote], [ImportedTime], [Name], [Polygon], [Type])
VALUES (''00000001-0000-0000-0000-000000000000'', N''Seeded.'', NULL, NULL, N''Trondheim'', geography::Parse(''POLYGON ((10.0358 63.2433, 10.5669 63.2433, 10.5669 63.473, 10.0358 63.473, 10.0358 63.2433))''), ''City''),
```

2nd scaffolded SQL script:

```sql
IF NOT EXISTS(SELECT * FROM [api].[_migration_history] WHERE [MigrationId] = N'20220204134433_TestMigration')
BEGIN
EXEC(N'UPDATE [api].[geo_areas] SET [Polygon] = geography::Parse(''POLYGON ((10.0358 63.2433, 10.5669 63.2433, 10.5669 63.473, 10.0358 63.473, 10.0358 63.2433))'')
WHERE [Id] = ''00000001-0000-0000-0000-000000000000'';
SELECT @@ROWCOUNT');
END;
GO
```

### Include provider and version information

EF Core version: 6.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: 6.0
Operating system: Win 11, Mac
IDE: Rider 2021.3.3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.