Linq2Sql 或 EF4 中的空间数据类型支持

发布于 2024-08-02 18:40:14 字数 86 浏览 6 评论 0原文

有谁知道(最好是有参考)VS2010 版本的 LinqToSQL 或 EntityFramework v4 是否支持对 SQL 2008 空间数据类型的查询?

Does anyone know (ideally, with a reference), whether the VS2010 release of LinqToSQL or EntityFramework v4 will support queries over the SQL 2008 spatial data types?

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

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

发布评论

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

评论(3

别念他 2024-08-09 18:40:14

这是让它在实体框架/LINQ to Entities 中工作的解决方法:

您可以使用数据库视图返回众所周知的文本(在查询中使用“geometry.ToString()”)或二进制。然后,一旦返回结果行,只需将字符串/二进制文件转换为 .NET 中的 SqlGeometry 对象即可。

以下是用于构建视图的示例查询,该视图将几何类型的“位置”字段转换为众所周知的文本字符串:

SELECT ID, Name, Location.ToString() as Location FROM MyTable

以下是查询具有包含众所周知的“位置”字段的结果实体的示例。 “地理”对象的文本或字符串表示形式:

var e = new MyApp.Data.MyDataEntities(connectionString);
var items = from i in e.MyTables
            select i;

foreach (var i in items)
{
    // "Location" is the geography field
    var l = SqlGeography.Parse(i.Location);
    var lat = l.Lat;
    var lng = l.Long;
}

另一件事是,您需要在存储过程中执行任何基于空间的查询,因为您不想将表中的所有数据提取到 .NET 中以便使用 LINQ 执行您自己的空间查询。

这并不像原生支持 SQL 空间类型那样优雅,但它可以让您同时运行实体框架和 SQL 空间。

Here's a workaround to get it working in Entity Framework / LINQ to Entities:

You can use a database View to return Well-Known-Text (using "geometry.ToString()" in the query) or Binary. Then once the resulting rows are returned, just convert the string/binary to a SqlGeometry object in .NET.

Here's a sample query used to build a View that converts a "Location" field of geometry type to a Well-Known-Text String:

SELECT ID, Name, Location.ToString() as Location FROM MyTable

Here's an example of querying the resulting entities that have a "Location" field that contains a Well-Known-Text or String representation of the "geography" object:

var e = new MyApp.Data.MyDataEntities(connectionString);
var items = from i in e.MyTables
            select i;

foreach (var i in items)
{
    // "Location" is the geography field
    var l = SqlGeography.Parse(i.Location);
    var lat = l.Lat;
    var lng = l.Long;
}

One additional thing, is you'll need to do any spatial based queries within Stored Procedures, since you don't want to pull ALL the data from the table into .NET in order to perform your own spatial query using LINQ.

This isn't an elegent as natively supporting SQL Spatial Types, but it'll get you running with Entity Framework and SQL Spatial simultaneously.

时间海 2024-08-09 18:40:14

在 EF 4.0 中,您可以使用 自定义函数 并假装空间类型实际上是二进制类型。这是我正在考虑尝试并添加到 我的技巧系列。但到目前为止,即使是黑客攻击也尚未得到证实。 :(

至于直接支持,不幸的是 L2S 或 EF v4 都不支持 VS2010 时间范围内的空间类型。

Alex James

实体框架项目经理。

In EF 4.0 you might be able to hack something together using a combination of custom functions and pretending the spatial types are really Binary types. This is something that I am thinking of mucking around with and trying out and adding to my tips series. But as yet even the hack is unproven. :(

And as for direct support, unfortunately neither L2S or EF v4 will support spatial types in the VS2010 timeframe.

Alex James

Entity Framework Program Manager.

好多鱼好多余 2024-08-09 18:40:14

您当然也可以使用手写的表和列进行 Linq-to-SQL,并直接获取 SQL 空间类型。我在示例数据库上测试了以下内容(不要忘记包含对 System.SqlServer.Types 的引用和“使用

……

string connectionString = @"Data Source=YADDAYADDA;Initial Catalog=MUMBLEMUMBLE;Integrated Security=True";
var pointsFileDc = new PointsFileDC(connectionString);
var geos = (from point in pointsFileDc.pointsData
            select point).Take(10);
foreach (var geo in geos)
{
    ObjectDumper.Write(geo);
}

public class PointsFileDC : DataContext
{
    public Table<GeoPoints> pointsData;
    public PointsFileDC(string connection)
        : base(connection)
    {
    }
}

[Table(Name = "Points")]
public class GeoPoints
{
    [Column(IsPrimaryKey = true)]
    public int PointId;
    [Column]
    public SqlGeography GeoPoint;
}

You can also definitely do Linq-to-SQL with hand-written tables and columns and get the SQL spatial types directly. I tested the following on a sample DB (dont' forget to include reference and 'using' to System.SqlServer.Types

...

string connectionString = @"Data Source=YADDAYADDA;Initial Catalog=MUMBLEMUMBLE;Integrated Security=True";
var pointsFileDc = new PointsFileDC(connectionString);
var geos = (from point in pointsFileDc.pointsData
            select point).Take(10);
foreach (var geo in geos)
{
    ObjectDumper.Write(geo);
}

...

public class PointsFileDC : DataContext
{
    public Table<GeoPoints> pointsData;
    public PointsFileDC(string connection)
        : base(connection)
    {
    }
}

[Table(Name = "Points")]
public class GeoPoints
{
    [Column(IsPrimaryKey = true)]
    public int PointId;
    [Column]
    public SqlGeography GeoPoint;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文