Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何使用AutoMapper将json请求dto中的OData枚举字符串映射到实体枚举属性_Asp.net Core_Json.net_Entity Framework Core_Odata_Automapper - Fatal编程技术网

Asp.net core 如何使用AutoMapper将json请求dto中的OData枚举字符串映射到实体枚举属性

Asp.net core 如何使用AutoMapper将json请求dto中的OData枚举字符串映射到实体枚举属性,asp.net-core,json.net,entity-framework-core,odata,automapper,Asp.net Core,Json.net,Entity Framework Core,Odata,Automapper,我正在使用Microsoft.AspNetCore.OData V7.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端点时,我遇到以下错误 AutoMapper.AutoMapperMappingException: Missing type map configuration or uns

我正在使用Microsoft.AspNetCore.OData V7.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端点时,我遇到以下错误

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
我在创建此问题时查看了类似的问题列表,但无法找到解决方案。 在回顾谷歌搜索AutoMapper OData Enum时,我所能找到的只是建议用

[AutoMap(typeof(Account))]
。。。并用…装饰我的dto枚举属性

[JsonConverter(typeof(StringEnumConverter))]
然而,我仍然得到了错误。我找到了使用AutoMapperProfile类的引用,该类的映射器定义为

CreateMap<Account, AccountModel>().ReverseMap();
这是我的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);

}
这里是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; }
}
这是我的帐户到班级

[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; }
}
这是我的AccountType和StatusType的枚举

public enum AccountTypes
{
    Customer = 0,
    Reseller = 1,
    Provider = 2,
}

public enum StatusTypes
{
    Active = 0,
    Inactive = 1,
}

有什么想法吗?

事实证明,我需要创建一个AutoMapper MapperConfiguration的实例,并将其分配给映射器

例如,我最终加入了控制器的构造函数

    public AccountsController(CdContext context, IMapper mapper)
    { 
        _context = context ?? throw new ArgumentNullException(nameof(context));
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
        var config = new MapperConfiguration(cfg => cfg.CreateMap<Account, AccountModel>().ReverseMap());
        _mapper = new Mapper(config);
    }
公共帐户控制器(CdContext上下文,IMapper映射程序) { _context=context??抛出新ArgumentNullException(nameof(context)); _mapper=mapper??抛出新的ArgumentNullException(nameof(mapper)); var config=new-MapperConfiguration(cfg=>cfg.CreateMap)自动映射主题上的文档

public enum AccountTypes
{
    Customer = 0,
    Reseller = 1,
    Provider = 2,
}

public enum StatusTypes
{
    Active = 0,
    Inactive = 1,
}
    public AccountsController(CdContext context, IMapper mapper)
    { 
        _context = context ?? throw new ArgumentNullException(nameof(context));
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
        var config = new MapperConfiguration(cfg => cfg.CreateMap<Account, AccountModel>().ReverseMap());
        _mapper = new Mapper(config);
    }