簡體   English   中英

無法使用dapper將SQL Geography列映射到EF DbGeography類

[英]Cannot map SQL Geography column to EF DbGeography class with dapper

我的SQL數據庫中有帶有sql地理列的表。 我已經使用EF6為數據庫生成了實體。 如您所知,Entitiy Framework會為SQL地理類型生成System.Data.Entity.Spatial.DbGeography 我正在使用dapper運行查詢並將結果映射到我的EF生成的實體中。

我的實體班

public partial class Fix
{
    public Fix()
    {
        this.FixUsers = new HashSet<FixUser>();
    }

    public long FixID { get; set; }
    public long UserID { get; set; }
    public System.Data.Entity.Spatial.DbGeography Position { get; set; }
    public int Radius { get; set; }
    public System.DateTime CreatedDate { get; set; }

    public virtual MemberProfile MemberProfile { get; set; }
    public virtual ICollection<FixUser> FixUsers { get; set; }
}

SQL查詢拋出異常

    var fix = SqlConnection.Query<Fix>(@"SELECT TOP(1) 
                                         f.FixID as FixID, 
                                         f.UserID as UserID, 
                                         f.Radius as Radius, 
                                         f.CreatedDate as CreatedDate, 
                                         f.Position as Position
                                         FROM [Fix] f 
                                         WHERE f.FixID = @fixId", new { fixId }).FirstOrDefault();

這是異常快照

在此處輸入圖片說明

我認為默認情況下,dapper試圖映射到Microsoft.SqlServer.Types.SqlGeography

這里有什么解決方法嗎?

已編輯

找到一些解決方案,為我的實體創建了部分類

public partial class Fix
{
    public string PositionString
    {
        set
        {
            Position = DbGeography.PointFromText(value, 4326);
        }
    }
}

並更改了我的查詢

var fix = SqlConnection.Query<Fix>(@"SELECT TOP(1) 
                                     f.FixID as FixID, 
                                     f.UserID as UserID, 
                                     f.Radius as Radius, 
                                     f.CreatedDate as CreatedDate, 
                                     f.Position.ToString() as PositionString
                                     FROM [Fix] f 
                                     WHERE f.FixID = @fixId", new { fixId }).FirstOrDefault();

如果您真的想使用Dapper,可以將SqlGeography轉換為DbGeography:

DbGeography.FromText(sqlGeo.ToString());

因此,只需在內存中進行轉換,或者也可以將SQL與EF一起使用:

dbContext.Fixes.SqlQuery("SELECT TOP(1) 
                                     f.FixID, 
                                     f.UserID, 
                                     f.Radius, 
                                     f.CreatedDate, 
                                     f.Position
                                     FROM [Fix] f 
                                     WHERE f.FixID = @fixId", new SqlParameter("fixId", fixId)).FirstOrDefault();

還是僅以正常方式使用實體框架? 由於它不是一個復雜的查詢:)

dbContext.Fixes.Find(fixId);

我仍然很好奇為什么要使用Dapper進行查詢,然后將其從EF映射到實體

Dapper內置了對許多常見數據類型的支持,但不是全部。 您可能會考慮使用自定義查詢參數-您可以查看如何通過此提交為表值參數(如DataTable添加自定義支持。 我非常不願意添加任何需要額外依賴的東西,尤其是對於EF之類的東西。 將來可能有用的是用於自定義提供程序的可自定義注冊工具(允許參數中的數據為任意值-將地圖移動到發生位置)。 但是今天這還不存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM