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

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:46:35

问题


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.

I added the following to my model.

using NetTopologySuite.Geometries;


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

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

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


回答1:


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.




回答2:


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();
        ...
    }
    ...
}


来源:https://stackoverflow.com/questions/53688803/entity-framework-core-ef-core-2-2-point-boundary-is-of-an-interface-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!