NpgSql,EF6,NTS:EntityType'Point'没有定义键

时间:2018-10-03 08:58:24

标签: entity-framework-6 postgis npgsql

我正在使用C#编写的.NET Framework 4.7简单导入工具,以读取MS-Access文件并将其保存到PostGIS数据库中。

现在让EF6(我的Point对象是NetTopology类而不是另一个表)遇到了问题。

当我从PostGIS数据库读取数据时,出现此错误:

System.Data.Entity.ModelConfiguration.ModelValidationException
  HResult=0x80131500
  Message=One or more validation errors were detected during model generation:

Importer.Point: : EntityType 'Point' has no key defined. Define the key for this EntityType.
Points: EntityType: EntitySet 'Points' is based on type 'Point' that has no keys defined.

以下是一些代码:

    public static void Main(string[] args)
    {
        // Place this at the beginning of your program to use NetTopologySuite everywhere (recommended)
        NpgsqlConnection.GlobalTypeMapper.UseNetTopologySuite();

        using (var context = new PostGisContext())
        {
            var newKey = context.MyLocations.Count();
        }
    }

context.MyLocations.Count()抛出错误。

public class PostGisContext : DbContext
{
    public DbSet<MyLocation> MyLocations { get; set; }

    public PostGisContext() : base("name=PostgreSql")
    {        }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // PostgreSQL uses the data schema by default - not dbo.
        modelBuilder.HasDefaultSchema("data");

        // Don't use plural table names:
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        // Change CamelCase to lower-case:
        modelBuilder.Types().Configure(c => c.ToTable(LowerCaseTableName(c.ClrType)));

        modelBuilder.Conventions.Add(new LowerCaseConvention());

        base.OnModelCreating(modelBuilder);
    }

    private static string LowerCaseTableName(Type type)
    {
        var result = Regex.Replace(type.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]);

        return result.ToLower();
    }
}

// A custom convention.
internal class LowerCaseConvention : IStoreModelConvention<EdmProperty>
{
    public void Apply(EdmProperty property, DbModel model)
    {
        property.Name = Regex.Replace(property.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]).ToLower();
    }
}

Location列更改为字符串时,上述代码可以正常工作。

public class MyLocation
{
    [Key] 
    public int Id { get; set; }
    public string LocationId { get; set; }
    public NetTopologySuite.Geometries.Point Location { get; set; }
} 

public class NpgSqlConfiguration : DbConfiguration
{
    public NpgSqlConfiguration()
    {
        const string name = "Npgsql";

        SetProviderFactory(providerInvariantName: name, 
            providerFactory: NpgsqlFactory.Instance);

        SetProviderServices(providerInvariantName: name, 
            provider: NpgsqlServices.Instance);

        SetDefaultConnectionFactory(connectionFactory: new NpgsqlConnectionFactory());
    }
}

我一定会忘记设置或其他东西,但找不到。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

仅在Entity Framework Core中,实体框架6不支持NetTopologySuite和PostGIS。这是因为EF6的类型系统是封闭的,并且不允许像EF Core那样灵活地进行提供商特定的扩展。