【发布时间】:2020-03-23 07:47:57
【问题描述】:
我正在使用 .NetCore 和 EntityFramework Core 创建一个 Web API。 我有一个基于此模型的对象:
public partial class Person
{
public int Person_Id { get; set; }
public string Nickname { get; set; }
public string City { get; set; }
}
我有这样的资源:
public string Property { get; set; }
public string V1 { get; set; }
public string V2 { get; set; }
我想使用 Automapper 将模型的每个属性(Person_Id、Nickname、City)映射到资源的 IEnumerable,这样我就可以实现这样的目标:
[
{
Property: Person_Id,
V1: null,
V2: null
},
{
Property: Nickname,
V1: null,
V2: null
},
{
Property: City,
V1: null,
V2: null
}
]
因此每个属性都成为 IEnumerable 中的一个新条目。 我该怎么做?
【问题讨论】:
-
docs.automapper.org/en/latest/Dynamic-and-ExpandoObject-Mapping.html
-
您能否指定来自 DB(实体)的内容以及您希望的模型是什么?
-
@LucianBargaoanu 我不明白这对我有什么帮助,抱歉
-
@juagicre 数据库提供了一个人(即 Person_Id:1,昵称:“John”,城市:“New York”),我想要一个
IEnumerable<Resource>(即[ { Property: Person_Id, V1: "1", V2: null }, { Property: Nickname, V1: "John", V2: null }, { Property: City, V1: "New York", V2: null } ] -
你是使用EF获取数据库数据还是通过json中的API获取?
标签: c# .net-core automapper