Entity Framework Core - EF Core 2.2 - 'Point.Boundary' is of an interface type ('IGeometry')
Asked Answered
B

2

8

I am trying the new functionality with EF Core 2.2. It is based on the following article. "Announcing Entity Framework Core 2.2" https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-entity-framework-core-2-2/

I installed the following Nuget package.

enter image description here

I added the following to my model.

using NetTopologySuite.Geometries;


//New as of EF.Core 2.2 
//[Required] 
//[NotMapped] 
public Point Location { get; set; }

enter image description here

During my application startup I get the following error in my Database Context on the following line: Database.EnsureCreated();

enter image description here

System.InvalidOperationException HResult=0x80131509 Message=The property 'Point.Boundary' is of an interface type ('IGeometry'). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type, otherwise ignore the property using the NotMappedAttribute or 'EntityTypeBuilder.Ignore' in 'OnModelCreating'. Source=Microsoft.EntityFrameworkCore

Bedside answered 9/12, 2018 at 2:9 Comment(1)
I am also having this problem. .net core 2.2 with entity framework core 2.2. I get the error when running add-migrationMaddeu
M
15

You need to call UseNetTopologySuite(). Example here:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json")
           .Build();
        var connectionString = configuration.GetConnectionString("DefaultConnection");
        optionsBuilder.UseSqlServer(connectionString, opts => opts.UseNetTopologySuite());
    }
    public DbSet<Test> Tests { get; set; }
}


public class Test
{
    public int Id { get; set; }
    public Point Location { get; set; }
}

I ran into this problem because I had a if (!optionsBuilder.IsConfigured) around everything in my OnConfiguring. I had to remove this in order to get add-migrations to work.

Maddeu answered 10/12, 2018 at 23:22 Comment(0)
H
1

As Kyle pointed out you need to call UseNetTopologySuite(), but I would call it during ConfigureServices like this:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddEntityFrameworkNpgsql()
            .AddDbContext<MyDBContext>(opt =>
                opt.UseNpgsql(Configuration.GetConnectionString("MyDBConnection"),
                                o=>o.UseNetTopologySuite()))
            .BuildServiceProvider();
        ...
    }
    ...
}
Hyperactive answered 9/8, 2019 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.