【发布时间】:2020-05-30 11:46:02
【问题描述】:
我正在使用 Microsoft.AspNetCore.OData v 7.3.0、AutoMapper v9.0.0 和 Microsoft.AspNetCore.Mvc.NewtonsoftJson v3.1.1 开发新的 ASP.NET Core 3.1.1 API
当我使用 Postman v7.18.0 向 Accounts 端点发送 POST 时出现以下错误;
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
我在创建此问题时查看了类似问题列表,但无法找到解决方案。 在审查 Google 搜索 AutoMapper OData Enums 时,我能找到的只是建议用...装饰我的 dto 类。
[AutoMap(typeof(Account))]
...并用...装饰我的 dto 枚举属性
[JsonConverter(typeof(StringEnumConverter))]
但是,我仍然收到错误消息。我找到了使用 AutoMapperProfile 类的引用,其映射器定义为
CreateMap<Account, AccountModel>().ReverseMap();
但 AutoMapper v9.0.0 似乎不再具有 CreateMap 方法。我的理解是,将 [AutoMap(typeof(Account))] 添加到 dto 类与在配置文件类中创建地图的效果相同。
我觉得我现在在这里绕圈子,所以我想联系 SO 社区。我确信这很简单,我只是没有看到它。
这是我来自 Postman 的 POST 请求正文;
{
"@odata.context": "https://localhost:44367/v1/$metadata#Accounts",
"AccountName": "Test Provider",
"AccountType": "Provider",
"IsTaxExempt": false,
"Status": "Active"
}
这是我的 AccountsController Post 方法;
[ODataRoute]
[Produces("application/json;odata.metadata=minimal")]
[ProducesResponseType(typeof(AccountModel), Status201Created)]
[ProducesResponseType(Status400BadRequest)]
public async Task<IActionResult> Post([FromBody] AccountModel record)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
record.Id = new Guid();
var entity = _mapper.Map<Account>(record);
_context.Add(entity);
await _context.SaveChangesAsync();
var createdRecord = _mapper.Map<AccountModel>(entity);
return Created(createdRecord);
}
这是我的 Account 实体类;
public class Account : EntityBase
{
[Required]
[Column(TypeName = "nvarchar(50)")]
[MaxLength(50)]
public string AccountName { get; set; }
public AccountTypes AccountType { get; set; }
public bool IsTaxExempt { get; set; }
}
这里是EntityBase类;
public class EntityBase
{
[Required]
public Guid Id { get; set; }
public DateTimeOffset? DateTimeCreated { get; set; } = DateTime.UtcNow;
public DateTimeOffset? DateTimeLastModified { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public StatusTypes Status { get; set; }
public bool DeleteFlag { get; set; }
}
这是我的 Account DTO 类;
[Filter, Count, Expand, OrderBy, Page, Select]
[AutoMap(typeof(Account))]
public class AccountModel : BaseModel
{
[Required]
[MaxLength(50)]
public string AccountName { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public AccountTypes AccountType { get; set; }
public bool IsTaxExempt { get; set; }
}
这是我的 BaseModel 类;
[Select, Filter]
public class BaseModel
{
public Guid Id { get; set; }
public DateTimeOffset DateTimeCreated { get; set; } = DateTime.UtcNow;
public DateTimeOffset DateTimeLastModified { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public StatusTypes Status { get; set; }
public bool DeleteFlag { get; set; }
}
这是我的 AccountTypes 和 StatusTypes 枚举
public enum AccountTypes
{
Customer = 0,
Reseller = 1,
Provider = 2,
}
public enum StatusTypes
{
Active = 0,
Inactive = 1,
}
有什么想法吗?
【问题讨论】:
标签: asp.net-core json.net entity-framework-core odata automapper