EF核心3.0在插入记录'找不到实体类型'ChannelTypeT'的属性'Id'时给出错误'

时间:2019-12-12 12:51:18

标签: c# asp.net-core automapper ef-core-3.0

我有带有自动映射器的.net core 3.0 API。

我的映射出现一个问题。下面我列出了我的实体。

[Table(name: "ChannelType")]
public class ChannelType : FullAuditedEntity<string>
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Required]
    [StringLength(06)]
    public override string Id { get; set; }

    public string Text { get; set; }

    public virtual ChannelTypeT ChannelTypeT { get; set; }
}

ChannelTypeT实体将是这样。

[Table(name: "ChannelTypeT")]
public class ChannelTypeT
{
    [StringLength(02)]
    public string Lang { get; set; }
    public string Text { get; set; }

    #region Primary Key derived from Foreign key for Config tabels        
    [Required]
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    [ForeignKey(name: "ChannelTypeId")]
    public string ChannelTypeId { get; set; }
    public virtual ChannelType ChannelType { get; set; }
    #endregion
}

现在,这很简单,这意味着当我插入一个ChannelType时,代码也应该在ChannelTypeT中插入一条新记录,其ID与ChannelTypeId相同。这就是快照。

enter image description here

这是我的 Dto类。

public class ChannelTypeDtoIncluding
{
    public string Id { get; set; }
    public string Text { get; set; }

    public virtual ChannelTypeTDto ChannelTypeT { get; set; }
}

public class ChannelTypeTDto
{
    public string ChannelTypeId { get; set; }
    public string Lang { get; set; }
    public string Text { get; set; }
}

我的自动映射器映射。

CreateMap<ChannelTypeT, ChannelTypeTDto>().ReverseMap();
        CreateMap<ChannelType, ChannelTypeDtoIncluding>()
            .ForMember(d => d.Id, opt => opt.MapFrom(s => s.ChannelTypeT.ChannelTypeId))
            .ForMember(d => d.Id, opt => opt.MapFrom(s => s.ChannelTypeT.Id))
            .ReverseMap();

最后这是我的发布方法。

 [HttpPost("InsertIncluding")]
    public IActionResult PostIncluding([FromBody]ChannelTypeDtoIncluding model)
    {
        if (string.IsNullOrEmpty(model.Id))
            return new NotFoundResult();

        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        Response<ChannelType> itemToReturn = _service.Insert(_mapper.Map<ChannelTypeDtoIncluding, ChannelType>(source: model));
        if (itemToReturn.IsSuccess == true)
            return new OkObjectResult(_mapper.Map<ChannelType, ChannelTypeDtoIncluding>(source: itemToReturn.Data));
        else
            return new ObjectResult(itemToReturn.ErrorMessage) { StatusCode = 500 };
    }

但是在此方法中,当我尝试映射模型时,它给了我错误。下面是错误说明。

  

找不到实体类型“ ChannelTypeT”上的属性“ Id”。确保该属性存在并已包含在模型中。

我的映射器会将 ChannelType-> ID更新为ChannelTypeT-> ChannelTypeId 。 我认为问题出在映射器配置上,但是不确定我在哪里做错了。

任何对此的想法都会有很大帮助。

2 个答案:

答案 0 :(得分:2)

ChannelTypeChannelTypeT创建映射,并使用ForMember指定所需的连接

CreateMap<ChannelType, ChannelTypeT>()
    .ForMember(c => c.ChannelTypeId, m => m.MapFrom(ct => ct.Id));

答案 1 :(得分:2)

这是一个正在运行的演示,您可以参考

  1. 对于ChannelTypeId模型中的ForeignKey ChannelTypeT,应按如下所示进行修改,请参考ForeignKey Attribute

    [Table(name: "ChannelTypeT")]
    public class ChannelTypeT
    {
        [StringLength(02)]
        public string Lang { get; set; }
        public string Text { get; set; }
    
        #region Primary Key derived from Foreign key for Config tabels        
        [Required]
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        [ForeignKey(name: "ChannelType")]
        public string ChannelTypeId { get; set; }
        public virtual ChannelType ChannelType { get; set; }
        #endregion
    }
    
  2. 映射文件

    public class MapperProfile: Profile
    {
       public MapperProfile()
      {
        CreateMap<ChannelTypeT, ChannelTypeTDto>().ReverseMap();
        CreateMap<ChannelType, ChannelTypeDtoIncluding>()
            .ForMember(d => d.Text, opt => opt.MapFrom(s => s.ChannelTypeT.Text))
            .ReverseMap();
      }
    }
    
  3. 发布方法

    [HttpPost]
    [Route("/InsertIncluding")]
    public IActionResult PostIncluding([FromBody]ChannelTypeDtoIncluding model)
    {
        if (string.IsNullOrEmpty(model.Id))
            return new NotFoundResult();
    
        if (!ModelState.IsValid)
            return BadRequest(ModelState);
    
        ChannelType data = _mapper.Map<ChannelTypeDtoIncluding, ChannelType>(source: model);
        _context.ChannelType.Add(data);
        _context.SaveChanges();
        return new OkObjectResult(_mapper.Map<ChannelType, ChannelTypeDtoIncluding>(source: data));
    }
    

4。结果: enter image description here