Esempio n. 1
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            SpatialEntities = new Dictionary <Type, SqlServerSpatialEntityMetadata>();
            foreach (var(entity, property, geometryType) in SqlServerSpatialColumn.GetSpatialColumns(modelBuilder.Model))
            {
                var tableName = modelBuilder.Entity(entity.ClrType).Metadata.SqlServer().TableName;
                var keyName   = entity.FindPrimaryKey().Properties[0].Name;
                var metadata  = new SqlServerSpatialEntityMetadata(entity, tableName, keyName, property.Name, geometryType, entity.GetProperties().ToList());
                SpatialEntities.Add(entity.ClrType, metadata);

                var propertyBuilder = modelBuilder
                                      .Entity(entity.Name)
                                      .Property(property.Name);

                if (propertyBuilder.Metadata.ClrType != typeof(string))
                {
                    throw new InvalidCastException($"Field '{property.Name}' decorated with attribute '{nameof(SqlServerSpatialColumnAttribute)}' is defined as '{propertyBuilder.Metadata.ClrType.Name}' but must be defined as 'string' [{entity.Name}]");
                }

                propertyBuilder
                .HasColumnType("geometry")     // Force column to be the SQL Server spatial data type
                .IsRequired(false);
                propertyBuilder.Metadata.BeforeSaveBehavior = PropertySaveBehavior.Ignore;

                PerformAdditionalSpatialColumnProcessing(modelBuilder, entity, property, metadata);
            }
        }
Esempio n. 2
0
        private void EnsureSpatialIndexExists(SqlServerSpatialEntityMetadata metadata)
        {
            // Ensure spatial index exists, create it if not
            var spatialIndexName = $"{metadata.TableName}{metadata.GeometryFieldName}SpatialIndex";
            var sql = $@"
if not exists (select 1 from sys.indexes where name = '{spatialIndexName}')
begin
create spatial index [{spatialIndexName}]
on [{metadata.TableName}]([{metadata.GeometryFieldName}])
using geometry_auto_grid
with (bounding_box = {SpatialIndexBoundingBox})
end
";

#pragma warning disable EF1000 // Possible SQL injection vulnerability.
            Database.ExecuteSqlCommand(sql);
#pragma warning restore EF1000 // Possible SQL injection vulnerability.
        }
Esempio n. 3
0
 protected virtual void PerformAdditionalSpatialColumnProcessing(ModelBuilder modelBuilder, IEntityType entityType, PropertyInfo property, SqlServerSpatialEntityMetadata metadata)
 {
 }