Skip to content

Code-first spatial index creation  #26

@jamesra

Description

@jamesra

I am getting this error creating my database from scratch in a unit test:

Microsoft.Data.SqlClient.SqlException: 'Column 'MosaicShape' in table 'Location' is of a type that is invalid for use as a key column in an index or statistics.'

This is a simplified version of the class. It was reverse-engineered by EF Core Power Tools.

[Table("Location")]
[Index(nameof(MosaicShape), Name = "MosaicShape_Index")]
class Location{

 [Key]
 [Column("ID")]
 public long Id { get; set; }

 [Required]
 [Column(TypeName = "geometry")]
 public Geometry MosaicShape { get; set; }
} 

In a unit test I attempt to create this database from scratch. The error occurs at DataContext.Database.EnsureCreated();

public CreateDropDatabaseFixture(IConfiguration configuration, ILogger log = null)
{
    var connStringTemplate = configuration.GetConnectionString("AnnotationConnection");

    DatabaseName = RandomLetters(6);
    var connString = string.Format(connStringTemplate, DatabaseName);

    DbContextOptionsBuilder<AnnotationContext> builder = new DbContextOptionsBuilder<AnnotationContext>();
    builder = builder.UseSqlServer(connString, config => config.UseNetTopologySuite()).EnableDetailedErrors().EnableSensitiveDataLogging();
    DataContext = new AnnotationContext(builder.Options, log);
     
    DataContext.Database.EnsureCreated();
}

This previous issue indicates a spatial index should be possible. dotnet/efcore#12538

I also tried removing the [Index] attribute from the class and creating it with the builder:

entity.HasIndex(e => e.MosaicShape, "MosaicShape_Index");

The error was the same, as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions