-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
SpatialQuerySqliteFixture.cs
61 lines (51 loc) · 2.43 KB
/
SpatialQuerySqliteFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestModels.SpatialModel;
using NetTopologySuite.Geometries;
namespace Microsoft.EntityFrameworkCore.Query;
public class SpatialQuerySqliteFixture : SpatialQueryRelationalFixture
{
protected override ITestStoreFactory TestStoreFactory
=> SqliteTestStoreFactory.Instance;
protected override IServiceCollection AddServices(IServiceCollection serviceCollection)
=> base.AddServices(serviceCollection)
.AddEntityFrameworkSqliteNetTopologySuite()
.AddSingleton<IRelationalTypeMappingSource, ReplacementTypeMappingSource>();
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
{
var optionsBuilder = base.AddOptions(builder);
new SqliteDbContextOptionsBuilder(optionsBuilder).UseNetTopologySuite();
return optionsBuilder;
}
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);
modelBuilder.HasDbFunction(
typeof(GeoExtensions).GetMethod(nameof(GeoExtensions.Distance)),
b => b.HasTranslation(
e => new SqlFunctionExpression(
"Distance",
arguments: e,
nullable: true,
argumentsPropagateNullability: e.Select(a => true).ToList(),
typeof(double),
null)));
}
private class ReplacementTypeMappingSource : SqliteTypeMappingSource
{
public ReplacementTypeMappingSource(
TypeMappingSourceDependencies dependencies,
RelationalTypeMappingSourceDependencies relationalDependencies)
: base(dependencies, relationalDependencies)
{
}
protected override RelationalTypeMapping FindMapping(in RelationalTypeMappingInfo mappingInfo)
=> mappingInfo.ClrType == typeof(GeoPoint)
? ((RelationalTypeMapping)base.FindMapping(typeof(Point))
.WithComposedConverter(new GeoPointConverter()))
.WithStoreTypeAndSize("geometry", null)
: base.FindMapping(mappingInfo);
}
}