Entity Framework Core:此平台不支持 Udt 类型.(空间数据 - 地理) [英] Entity Framework Core: Type Udt is not supported on this platform. (Spatial Data - Geography)

查看:27
本文介绍了Entity Framework Core:此平台不支持 Udt 类型.(空间数据 - 地理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验实体框架核心并偶然发现了一个我以前从未见过的错误,无法弄清楚如何修复它.我正在使用 .net Core Web API 2.0 和 EntityFramework Core 2.00-preview2-final

I'm experimenting with entity framework core and stumbled upon an error I've never seen before and can't figure out how to fix it. I'm using .net Core Web API 2.0 with EntityFramework Core 2.00-preview2-final

这是一个触发错误的简单示例.

Here is a simple example that triggers the error.

(概念:从数据库中获取用户的简单端点)

(concept: simple endpoint to get a user from database)

错误:System.PlatformNotSupportedException: 此平台不支持 Udt 类型.

Error: System.PlatformNotSupportedException: Type Udt is not supported on this platform.

有什么建议吗?

问题是我在我的数据库中使用 geography 但我在我的模型中将它用作字符串,因为实体框架核心还不支持空间数据...

有什么方法可以在不摆脱地理因素的情况下保持这个蛋糕的美味,因为它是一个重要的特征?

Any way to keep this cake tasty without getting rid of geography, cause it's an important feature?

查看我对当前解决方案的回答

Edit : See my answer for current solution

推荐答案

好的,我是这样解决的:

Ok here is how I solved it:

目的是在 Entity Framework Core 中保留 geography(不使用 DbGeography)

The purpose is to keep geography in Entity Framework Core (without using DbGeography)

1) 我创建了一个名为 Location 的结构:

1) I created a struct called Location:

public struct Location
{
    public double Longitude { get; set; }
    public double Latitude { get; set; }
}

2) 将其添加到您的 EF 实体模型

2) Add it to your EF Entity Model

public class User
{
    public Location Location { get; set; }
}

3) 将其隐藏在您的模型构建器中

3) Hide it in your modelbuilder

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>().Ignore(x => x.Location);
}

4) 生成迁移(Add-Migration migrationname)

4) Generate a Migration (Add-Migration migrationname)

5) 转到您的迁移文件 1231randomnumbers1231_migrationname.cs并添加以下内容(这样我们创建了另一列名为 Location 的 geography 类型),然后更新您的数据库 (update-database):

5) Go to your migration file 1231randomnumbers1231_migrationname.cs and add the following (this way we create another column of type geography named Location) and then update your database (update-database):

migrationBuilder.Sql(@"ALTER TABLE [dbo].[User] ADD [Location] geography NULL");

6)(可选)我创建了一个静态类来更新数据库,如果您在多个表中有一个位置列,这很方便.

6) (optional) I created a static class to update the db, handy if you have a Location column in mulple tables.

public static class GeneralDB
{

    public static async Task UpdateLocation(DbContext ctx, string table, Location location, int id)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

        string query = String.Format(@"UPDATE [dbo].[{0}] SET Location = geography::STPointFromText('POINT(' + CAST({1} AS VARCHAR(20)) + ' ' + CAST({2} AS VARCHAR(20)) + ')', 4326) WHERE(ID = {3})"
        , table.ToLower(), location.Longitude, location.Latitude, id);
        await ctx.Database.ExecuteSqlCommandAsync(query);
    }
    public static async Task<Location> GetLocation(DbContext ctx, string table, int id)
    {
        Location location = new Location();

        using (var command = ctx.Database.GetDbConnection().CreateCommand())
        {
            string query = String.Format("SELECT Location.Lat AS Latitude, Location.Long AS Longitude FROM [dbo].[{0}] WHERE Id = {1}"
                , table, id);
            command.CommandText = query;
            ctx.Database.OpenConnection();
            using (var result = command.ExecuteReader())
            {
                if (result.HasRows)
                {
                    while (await result.ReadAsync())
                    {
                        location.Latitude = result.GetDouble(0);
                        location.Longitude = result.GetDouble(1);
                    }
                }

            }
        }

        return location;
    }
}

这只适用于 EF Core 2.0

This only works in EF Core 2.0

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

对于 EF Core 1.0,您必须找到一种替代方法来将,"替换为.".一个很好的老式 .Replace() 方法可以完成这项工作.

For EF Core 1.0 you would have to find an alternative way to replace a ',' with '.'. A good old fashion .Replace() method could do the job.

location.Longitude.ToString().Replace(',', '.')

7) CRUD 示例:

7) CRUD Examples:

7.1:阅读

public async Task<User> GetByIdAsync(int id)
{
    User user =  await ctx.User.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id);

    user.Location = await GeneralDB.GetLocation(ctx, "user", id);
    return user;
}

7.2:创建

public async Task<User> CreateAsync(User entity)
{

    ctx.User.Add(entity);
    await ctx.SaveChangesAsync();
    await GeneralDB.UpdateLocation(ctx, "user", entity.Location, entity.Id);
    return entity;  
}

7.3:更新

public async Task<User> UpdateAsync(User entity)
{
    ctx.User.Attach(entity);
    ctx.Entry<User>(entity).State = EntityState.Modified;
    await ctx.SaveChangesAsync();

    await GeneralDB.UpdateLocation(ctx, "user", entity.Location, entity.Id);

    return entity;
}

这篇关于Entity Framework Core:此平台不支持 Udt 类型.(空间数据 - 地理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆