Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 474
L. Piotrowski Avatar asked Dec 09 '18 02:12

L. Piotrowski


2 Answers

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.

like image 188
Kyle Avatar answered Sep 22 '22 12:09

Kyle


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();
        ...
    }
    ...
}
like image 20
igorushi Avatar answered Sep 23 '22 12:09

igorushi