Entity framework 在简单映射情况下,AutoMapper为所有值返回null

Entity framework 在简单映射情况下,AutoMapper为所有值返回null,entity-framework,automapper,identity,dto,Entity Framework,Automapper,Identity,Dto,我将Entity Framework 6与MVC和Web API一起使用,我一直在考虑是否在JSON API方面使用数据传输对象DTO。。 我遇到AutoMapper,并且已经手动编写了一些DTO,我仍然认为这应该是管理DTO的一个好方法。 因此,新的,通过Nuget安装,我尝试了我的第一个映射。从IdentityUser派生的mblUser类作为源,我需要一个简单的APIUser。下面是这些类: public class mblUser : IdentityUser { pu

我将Entity Framework 6与MVC和Web API一起使用,我一直在考虑是否在JSON API方面使用数据传输对象DTO。。 我遇到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;
            }
        }
    }
}
。。。这是映射代码:

        // 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,但文档对此问题还很不清楚

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

您需要在映射属性之前将其公开

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; }
}

您的代码中似乎出现了一些问题。是源类型mblUser的名称吗?还是用户?ApiUser属性都是私有的。如果你把它们公之于众,Automapper会把它们映射出来吗!很有眼光的艾米。。我现在要到明天才能试,但我敢打赌就是这样了。。!我试试看,然后回来。如果是这样的话,我的原始自制映射代码中出现了一个愚蠢的剪切粘贴错误。非常感谢。乐观地说,布雷蒂会把它作为一个答案提交的。是的。。就这样。。我向外面的世界道歉。。但是你知道有时候它只是盯着你的脸。。再次感谢艾米。布雷特
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; }
}