0

I am using EF Core 2.2 code first to create a table that has a NTS Point property in it. That works fine and creates a table in SQL with a 'Geographic' data type. Now I want to index that column from code first. I am just doing a basic:

entity.HasIndex(x => x.Point);

I am getting the following error:

Message: System.Data.SqlClient.SqlException : Column 'PointColumnName' in table 'TableName' is of a type that is invalid for use as a key column in an index or statistics.

Is there a way use code first to put an index on this column?

2
  • what is the type of Point Commented Dec 23, 2018 at 16:48
  • Sorry, it is a NetTopologySuite.Geometries Point. Commented Dec 23, 2018 at 19:00

1 Answer 1

1

The easiest way is to customize your migration script according to your need. For Instance:

migrationBuilder.Sql(
@"
    UPDATE Customer
    SET Name = FirstName + ' ' + LastName;
");

Or you can use more comprehensive way Custom Migration Operations

class MyMigrationsSqlGenerator : SqlServerMigrationsSqlGenerator
{
    public MyMigrationsSqlGenerator(
        MigrationsSqlGeneratorDependencies dependencies,
        IMigrationsAnnotationProvider migrationsAnnotations)
        : base(dependencies, migrationsAnnotations)
    {
    }

    protected override void Generate(
        MigrationOperation operation,
        IModel model,
        MigrationCommandListBuilder builder)
    {
        if (operation is CreateSpatialIndexOperation createSpatialIndexOperation)
        {
            Generate(createSpatialIndexOperation, builder);
        }
        else
        {
            base.Generate(operation, model, builder);
        }
    }

    private void Generate(
        CreateSpatialIndexOperation operation,
        MigrationCommandListBuilder builder)
    {
        var sqlHelper = Dependencies.SqlGenerationHelper;
        var stringMapping = Dependencies.TypeMappingSource.FindMapping(typeof(string));

        builder
            .Append("CREATE INDEX ")
            .Append(sqlHelper.DelimitIdentifier(operation.Name))
            ...
            .Append(stringMapping.GenerateSqlLiteral(...))
            .AppendLine(sqlHelper.StatementTerminator)
            .EndCommand();
    }
}

then use it like

 protected override void OnConfiguring(DbContextOptionsBuilder options)
   => options
        .UseSqlServer(connectionString)
        .ReplaceService<IMigrationsSqlGenerator, MyMigrationsSqlGenerator>();
Sign up to request clarification or add additional context in comments.

1 Comment

I am not using EF migrations, but I still appreciate this answer. I was just hoping there was a way to describe the model using fluent options that did not involve migrations or magic strings... but the answer just may be that this is not built into the provider yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.