Beispiel #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);
            }
        }
        public override Expression VisitColumn(ColumnExpression columnExpression)
        {
            base.VisitColumn(columnExpression);

            // Check if this is a spatial column, and alter the query to return the WKT if so
            if (SqlServerSpatialColumn.IsSpatialColumn(columnExpression.Property))
            {
                Sql.Append(".STAsText()");
            }
            return(columnExpression);
        }