- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含几何列的空间表。我的实体如下:
public class Tile
{
public DbGeometry Geometry { get; set; }
}
我正在使用数据传输对象将 Tile 类返回给我的客户端。我还从客户端创建 Tiles,因此我想从字符串(几何图形的众所周知的文本表示形式)转换为 DbGeometry 字段。这是我的 DTO:
public class TileDto
{
public String Geometry { get; set; }
}
现在我的第一个想法是创建自定义转换器,如下所示:
public class DbGeometryToStringConverter : ITypeConverter<DbGeometry, string>
{
public string Convert(ResolutionContext context)
{
var geom = (DbGeometry) context.SourceValue;
return geom.AsText();
}
}
public class StringToDbGeometryConverter : ITypeConverter<string, DbGeometry>
{
public DbGeometry Convert(ResolutionContext context)
{
var wkt = (string) context.SourceValue;
var geom = DbGeometry.FromText(wkt);
if (geom == null || !geom.IsValid) return null;
return geom;
}
}
然后,我使用以下方法将 AutoMapper 配置设置为在两者之间进行映射:
AutoMapper.Mapper.Initialize(cfg =>
{
// Configure our custom type converter for the DbGeometry class
cfg.CreateMap<DbGeometry, string>().ConvertUsing<DbGeometryToStringConverter>();
cfg.CreateMap<string, DbGeometry>().ConvertUsing<StringToDbGeometryConverter>();
cfg.CreateMap<Tile, TileDto>();
cfg.CreateMap<TileDto, Tile>();
});
但这给了我错误:类型“System.String”没有默认构造函数
我尝试使用 ResolveUsing 方法,但它也给了我一个 NullReferenceException。我已确保所有列都有几何列,因此我不确定它为何为空。
cfg.CreateMap<Tile, TileDto>()
.ForMember(tileDto => tileDto.Geometry,
map => map.ResolveUsing(tile => tile.Geometry.WellKnownValue));
这在 Azure 移动服务后端的 ASP.NET 应用程序中使用,因此调试器因错误而中断的 Controller 是:
public class TileController : TableController<TileDto>
{
private DatabaseContext _context;
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
_context = new DatabaseContext();
DomainManager = new TileDtoToTileMappedEntityDomainManager(_context, Request, Services);
}
public SingleResult<TileDto> GetTile(string id)
{
// MY ERROR HAPPENS HERE
return Lookup(id);
}
}
public class TileDtoToTileMappedEntityDomainManager : MappedEntityDomainManager<TileDto, Tile>
{
public override SingleResult<TileDto> Lookup(string id)
{
return LookupEntity(t => t.Id == id);
}
public override Task<TileDto> UpdateAsync(string id, Delta<TileDto> patch)
{
return UpdateEntityAsync(patch, id);
}
public override Task<bool> DeleteAsync(string id)
{
return DeleteItemAsync(id);
}
}
我使用 AutoMapper 3.2.1,因为 Azure 移动服务 SDK 依赖于它。我正在尝试在 Azure 移动服务中为我的实体创建 DTO,因此我不完全确定是否是我的 AutoMapper 配置错误,或者我的实体的设置方式是否错误。我按照 this post 中的步骤操作创建 DTO。我需要在 AutoMapper 中进行配置更改才能使其正常工作吗?
最佳答案
感谢@Backs,问题出在我的 AutoMapper 配置上。使用 MapFrom 方法而不是 ResolveUsing 就达到了目的。
这是我的配置:
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Tile, TileDto>()
.ForMember(tileDto => tileDto.Geometry,
map => map.MapFrom(tile => tile.Geometry.AsText()));
cfg.CreateMap<TileDto, Tile>()
.ForMember(tile => tile.Geometry,
map => map.MapFrom(tileDto => DbGeometry.FromText(tileDto.Geometry)));
cfg.CreateMap<Tile, TileDto>();
cfg.CreateMap<TileDto, Tile>();
});
关于c# - 如何使用 Automapper 将 DbGeometry 转换为 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062898/
我有个问题希望大家帮我解决 我有一个 DbGeometry 点(或 DbGeography,我可以同时使用两者),我想检查它是否在 DbGeometry 多边形(或者 DbGeography)内。 我
我是新手。我不知道如何使用 SqlSpatialFunction MakeValid。我有一个 DbGeometry,它是一个多边形。这个多边形无效,我想使其有效。 谁能解释一下如何使用 MakeVa
MSDN documentation只是将返回值定义为: A double value that specifies the distance between the two closest poin
我正在尝试解析显示为 DbGeometry 的多边形类(从 System.Data.Entity.Spatial )到一些 List 表示,但是失败了。 我试过: - 用 .ToList<>() 方法
我正在按照 John Papa 在其最新的 PluralSight 类(class)中概述的方法使用 Angular、Breeze 和 Web API 2 构建 SPA。 一切正常,我可以将信息提取、
SqlGeometry 和 DbGeometry 之间有没有简单的转换方法?我正在使用一个流行的 sql 空间帮助程序库,其中的所有函数都需要 SqlGeometry。但是,当我针对 ESRI Arc
我有一个包含几何列的空间表。我的实体如下: public class Tile { public DbGeometry Geometry { get; set; } } 我正在使用数据传输对象
我有一个这个模型: public partial class UserPosition { ... public DbGeometry Position { get; set; }
我对 WKT 和 DbGeometry 一起工作有疑问。当我像这个例子一样使用 POINT 作为几何体时,效果很好。 DbGeometry myGeometry1 = DbGeometry.FromT
我正在尝试计算折线的面积 string poly = "POLYGON ((637604.918432772 2230520.64934531, 637622.257266129 223041
我正在使用 Entity Framework 6 进行 crud 操作,包括 DbGeometry 属性。该属性包含一个从 WellKnownText 创建的多边形。 DbGeometry.Polyg
我在一个表中有许多空间实体,其中有一个名为 Boundaries 的 geometry 字段。我想生成一个具有简化形状/几何图形的 GeoJson 文件。 这是我的第一次尝试: var entitie
我正在尝试创建一个 DbGeometry 类型的多边形。它在我的本地计算机上运行良好,但在 Azure Web 角色上托管我的网站时,返回语句出现错误。 代码: string polygon = “P
如标题所示,我有一个代码,当数据库中没有几何图形并且 DbGeometry 获得空值时,我会收到带有空值的 DbNull 类型。但是当我想做一个 if 语句来检查 DbGeometry 是否等于 Db
我正在使用 Entity Framework 6.0 Code First 构建 OData v3 Web API。 一切正常,我可以将 CRUD 操作执行回 api 服务器。 但是,我使用的是空间类
好的,我正在使用 OData v4 和 WebApi 2.2,其中一些实体在坐标系 4674 中设置了 DbGeometry 属性。 假设我们有一个简单的实体,如下所示: public class A
我是一名优秀的程序员,十分优秀!