Search code examples
entity-framework-core-2.1asp.net-core-2.2

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


Solution

  • 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.