NHibernate:TINYINT 而不是几何

发布于 2024-11-01 15:08:25 字数 1134 浏览 5 评论 0原文

我正在使用 NHibernate 开发一个 C# 项目,我使用 fluant-nhibernate 和 autoMapping :

        FluentConfiguration configuration = Fluently.Configure()
            .Database(databaseConfig)
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>(mappingConfiguration).Conventions.Add<GeometryTypeConvention>()));

我有一个具有 IGeometry 属性的类,我已经使用 self Convention 类型配置了自动映射:

public class GeometryTypeConvention : IUserTypeConvention
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(p => p.Property.PropertyType == typeof (IGeometry));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(typeof(MsSql2008GeometryType));
    }
}

当我更新架构时,会创建数据库,但所有几何图形类中的属性设置为 TINYINT 列。

我在 上看到了几乎相同的问题http://www.klopfenstein.net/lorenz.aspx/null-geometry-values-in-nhibernate-spatial-for-mssql2008,但我使用的文件MsSql2008GeometryType.cs是正确的。

I'm working on a C# project using NHibernate, I use fluant-nhibernate with autoMapping :

        FluentConfiguration configuration = Fluently.Configure()
            .Database(databaseConfig)
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>(mappingConfiguration).Conventions.Add<GeometryTypeConvention>()));

I have a classes with IGeometry properties, I have configured automapping with a self Convention type :

public class GeometryTypeConvention : IUserTypeConvention
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(p => p.Property.PropertyType == typeof (IGeometry));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(typeof(MsSql2008GeometryType));
    }
}

When I update the schema, the database is created but all Geometry properties in classes are set as TINYINT columns.

I've seen almost the same problem on http://www.klopfenstein.net/lorenz.aspx/null-geometry-values-in-nhibernate-spatial-for-mssql2008, but the file MsSql2008GeometryType.cs I use is correct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-11-08 15:08:25

我遇到了同样的问题,我用这种方式解决了它(使用地理而不是几何,但它非常相似):

首先(此步骤是可选的),并且因为我需要使用 WGS84 坐标,所以我创建了以下类型:

public class Wgs84GeographyType : MsSql2008GeographyType
{
    protected override void SetDefaultSRID(GeoAPI.Geometries.IGeometry geometry)
    {
        geometry.SRID = 4326;
    }
}

然后我创建了一个与您的约定类似的约定,但指定了“CustomSqlType”方法:

public class Wgs84GeographyTypeConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (typeof(IGeometry).IsAssignableFrom(instance.Property.PropertyType))
        {
            instance.CustomType(typeof(Wgs84GeographyType));
            instance.CustomSqlType("GEOGRAPHY");
        }
    }
}

之后模式生成应该可以正常工作,没有任何问题。

I had the same problem, and I solved it this way (using geography instead of geometry, but it's very similar):

First (this step is optional), and because I was required to work with WGS84 coordinates, I created the following type:

public class Wgs84GeographyType : MsSql2008GeographyType
{
    protected override void SetDefaultSRID(GeoAPI.Geometries.IGeometry geometry)
    {
        geometry.SRID = 4326;
    }
}

Then I created a convention somehow similar to yours, but with the "CustomSqlType" method specified:

public class Wgs84GeographyTypeConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (typeof(IGeometry).IsAssignableFrom(instance.Property.PropertyType))
        {
            instance.CustomType(typeof(Wgs84GeographyType));
            instance.CustomSqlType("GEOGRAPHY");
        }
    }
}

Afterwards the schema generation should work without any issue.

猫九 2024-11-08 15:08:25

您应该使用 SpatialAuxiliaryDatabaseObject 才能正确生成空间相关架构。使用 Fluent NHibernate,这看起来像:

    .ExposeConfiguration(
        cfg =>
        {
            cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
            new SchemaExport(cfg).Create(true, true);
        })

另外,在数据库配置中设置方言:

    .Database(MsSqlConfiguration
        .MsSql2008
        .Dialect(typeof (MsSql2008GeometryDialect).AssemblyQualifiedName)

You should use SpatialAuxiliaryDatabaseObject in order to properly generate spatial related schema. Using Fluent NHibernate, this would look like:

    .ExposeConfiguration(
        cfg =>
        {
            cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
            new SchemaExport(cfg).Create(true, true);
        })

Also, set the dialect in the database configuration:

    .Database(MsSqlConfiguration
        .MsSql2008
        .Dialect(typeof (MsSql2008GeometryDialect).AssemblyQualifiedName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文