AutoMapper returns 简单映射情况下所有值的空值
AutoMapper returns null for all values in simple mapping case
我正在将 Entity Framework 6 与 MVC 和 Web API 一起使用,我一直在考虑是否为 JSON api 使用数据传输对象 (DTO)事情的一面..
我遇到了 AutoMapper 并手动编写了一些 DTO 的想法(仍然认为)这应该是管理 DTO 的好方法。
所以,新手,通过 Nuget 安装,我尝试了我的第一个映射。 mblUser class 派生自 IdentityUser 作为源,我想要一个简单的 APIUser 沿着线路..这里是 classes:
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;
}
}
}
}
.. 这是 API 用户:
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,但我也看到其他地方也需要它,所以我有点卡住了..
任何帮助将不胜感激。
您需要先创建您的属性 public,然后才能对其进行映射。
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; }
}
我正在将 Entity Framework 6 与 MVC 和 Web API 一起使用,我一直在考虑是否为 JSON api 使用数据传输对象 (DTO)事情的一面.. 我遇到了 AutoMapper 并手动编写了一些 DTO 的想法(仍然认为)这应该是管理 DTO 的好方法。 所以,新手,通过 Nuget 安装,我尝试了我的第一个映射。 mblUser class 派生自 IdentityUser 作为源,我想要一个简单的 APIUser 沿着线路..这里是 classes:
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;
}
}
}
}
.. 这是 API 用户:
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,但我也看到其他地方也需要它,所以我有点卡住了.. 任何帮助将不胜感激。
您需要先创建您的属性 public,然后才能对其进行映射。
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; }
}