【问题标题】:AutoMapper returns null for all values in simple mapping caseAutoMapper 在简单映射情况下为所有值返回 null
【发布时间】:2015-04-09 15:02:44
【问题描述】:

我正在使用带有 MVC 和 Web API 的 Entity Framework 6,我一直在考虑是否将数据传输对象 (DTO) 用于 JSON api 方面。 我遇到了 AutoMapper 并编写了一些 DTO 手动认为(仍然认为)这应该是管理 DTO 的好方法。 所以,新手,通过 Nuget 安装,我尝试了我的第一个映射。从 IdentityUser 派生的 mblUser 类作为源,我想要一个简单的 APIUser 走线..这里是类:

    public class mblUser : IdentityUser 
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<mblUser> manager, string authenticationType = DefaultAuthenticationTypes.ApplicationCookie) 
    {
        //authentication type added to provide for Bearer type of authentication. Still defaults to cookie though so as not to impact the code
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType );
        // Add custom user claims here
        return userIdentity;
    }
    [Display(Name = "Date of Birth IDVIEW")]
    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:d}",
        NullDisplayText = "Please Enter Your Date of Birth", 
        ApplyFormatInEditMode = true)]

    public DateTime? DateOfBirth { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName {get; set;}
    public string DisplayName { get; set; }
    public virtual Leader Leader { get; set;}
    public virtual Guardian Guardian { get; set; }
    public virtual Participant Participant { get; set; }

    [NotMapped]
    [Display(Name ="Full Name")]
    public string FullName
    {
        get
        {
            return FirstName + (MiddleName !=null ? " " + MiddleName : "") + (LastName != null ? " " + LastName :"");
        }
    }
    [NotMapped]
    [Display(Name ="Details Complete")]
    public bool DetailsComplete
    {
        get
        {
            if (string.IsNullOrEmpty(FullName.Trim()) || string.IsNullOrEmpty(Email) ) 
            {
                // set true if we have something of a name and an email address.. 
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

.. 这里是 APIUser:

    public class APIUser
{
    private string Id { get; set; }
    private string Email { get; set; }
    private string UserName { get; set; }
    private string Title { get; set; }
    private string FirstName { get; set; }
    private string MiddleName { get; set; }
    private string LastName { get; set; }
    private string DisplayName { get; set; }
    private string PhoneNumber { get; set; }
}

...这里是映射代码:

        // GET: api/userSettings/5
    [ResponseType(typeof(APIUser))]
    public async Task<IHttpActionResult> GetmblUser(string username)
    {
        mblUser User = await UserManager.FindByNameAsync(username);
        if (User == null)
        {
            return NotFound();
        }

        Mapper.CreateMap<mblUser, APIUser>();

        APIUser dto = Mapper.Map<APIUser>(User); <-- here all User members are valid and populated.

        return Ok(dto); <--at a breakpoint here.. all dto members are null.
    }

我想我一定是在做某种新手错误或其他什么,但不知道是什么?我确实想知道这是否是因为 mblUser 是从 IdentityUser 派生的,但文档在这个问题上还很不清楚。

出于兴趣,我确实发现了另一个似乎与 here 相关的问题,而且我的参考文献中确实有 AutoMapper.Net4 以及 AutoMapper,但我也看到其他地方需要它,所以有点卡住了.. 任何帮助将不胜感激。

【问题讨论】:

  • 您的代码中似乎有些东西是倒退的。源类型的名称是“mblUser”吗?还是用户?
  • 那么ApiUser 属性都是private。如果您将它们公开,Automapper 会映射它们吗?
  • 噢!很好的发现艾米..我直到明天才能尝试,但我敢打赌就是这样..!我会试试然后回来。 .如果是这样,那是我原来的自制映射代码中的一个愚蠢的剪切和粘贴错误。 .非常感谢。乐观地布雷特
  • 那我把它作为答案提交。

标签: entity-framework automapper identity dto


【解决方案1】:

您需要先公开您的属性,然后才能映射它们。

public class APIUser
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string DisplayName { get; set; }
    public string PhoneNumber { get; set; }
}

【讨论】:

  • 是的..就是这样..我向外面的世界道歉..但你知道有时它只是盯着你的脸是怎么回事..再次感谢艾米。布雷特
猜你喜欢
  • 1970-01-01
  • 2013-08-10
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2014-06-06
相关资源
最近更新 更多