获取空值userId或userName

时间:2018-07-05 07:49:32

标签: c# asp.net-web-api automapper

我有以下几行类,其中一些类是从自定义基类继承而来的, 实体框架核心类。我从数据库获取数据没有问题,但是触发HttpPost时发生了问题。在HttpPost操作上,用户类不返回在Dto类中实现的userId或userName值。
 这是从EF核心身份用户继承的用户类:-

        public class User : IdentityUser
        {
            [Required, MaxLength(20, ErrorMessage = "First Name should not exceed more than 20 characters")]
            public string FirstName { get; set; }
            [Required, MaxLength(20, ErrorMessage = "Last Name should not exceed more than 20 characters")]
            public string LastName { get; set; }
            public ICollection<TechPost> TechPosts { get; set; }
                               = new List<TechPost>();
            public ICollection<TravelPost> TravelPosts { get; set; }
                              = new List<TravelPost>();

        }

基类被多个子类继承:-

     public abstract class AuditableEntity
        {
            [Required(ErrorMessage ="You cannot leave Title of the Post blank")]
            [MaxLength(100, ErrorMessage ="The Max length of the Title should not exceed more than 100 characters")]
            public string Title { get; set; }
            [Required]
            public string Description { get; set; }
            public DateTime PostCreatedDate { get; set; }    
            public User User { get; set; }

        }

从可审核实体继承的TechPost类:-

    public class TechPost : AuditableEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid TechId { get; set; }

    }

存储库实现:-

 public void AddTechBlogPost(string UserId, TechPost TPost)
        {
            var user = GetUser(UserId);
            if(user != null)
            {
                if(TPost.TechId == Guid.Empty)
                {
                    TPost.TechId = Guid.NewGuid();
                }
                user.TechPosts.Add(TPost);
            }
        }
  public User GetUser(string UserId)
        {
            return _ctx.Users.FirstOrDefault(u => u.Id == UserId);

        }

自动映射:-

            CreateMap<TechPost, TechPostDto>()
                      .ForMember(dest => dest.Id, opt=>opt.MapFrom(src =>
                       src.User.Id));

 public abstract class TTPostCreationDto
    {
        [Required(ErrorMessage ="You should fill out a title")]
        [MaxLength(100, ErrorMessage ="The title should not exceed more than 100 characters.")]
        public string Title { get; set; }
        public DateTime PostCreatedDate { get; set; }
        public virtual string Description { get; set; }
    }


    public class TechPostCreationDto : TTPostCreationDto
    {
        public Guid TechId { get; set; }
    }

HttpPost代码实现:-

    [HttpPost()]
    public IActionResult TPostForCreationUser(string UserId, [FromBody] TechPostCreationDto TPost)
    {
        if (TPost == null)
        {
            return BadRequest();
        }
        if (TPost.Description == TPost.Title)
        {
            ModelState.AddModelError(nameof(TechPostCreationDto),
                "The provided description should be different from the Title");
        }
        if (!ModelState.IsValid)
        {
            return new UnprocessableEntityObjectResult(ModelState);
        }
        if (!_repo.UserExists(UserId))
        {
            return NotFound();
        }

        var TPostEntity = _mapper.Map<TechPost>(TPost);
        _repo.AddTechBlogPost(UserId, TPostEntity);

        if (!_repo.Save())
        {
            throw new Exception($"Creating a Post for the user {UserId} failed on save");
        }
        var TPostToReturn = _mapper.Map<TechPostDto>(TPostEntity);

        return CreatedAtRoute("GetTPostForUser", new { UserId, techId = TPostToReturn.TechId }, TPostToReturn);

    }


public class TechPostDto : TTGetPostsDto    {
    public Guid TechId { get; set; }

}

TechPostsDto类:-

public abstract class TTGetPostsDto
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime PostCreatedDate { get; set; }  
}

0 个答案:

没有答案