C# 自动映射将目标对象设置为空
我一直在关注这篇文章: 但是当我到达实际的映射部分时,我的目标是空的。 谁能告诉我我做错了什么 Startup.csC# 自动映射将目标对象设置为空,c#,asp.net-core,automapper,C#,Asp.net Core,Automapper,我一直在关注这篇文章: 但是当我到达实际的映射部分时,我的目标是空的。 谁能告诉我我做错了什么 Startup.cs public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether us
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<AppDbContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<ICreditCardApplicationRepository,
EntityFrameworkCreditCardApplicationRepository>();
services.AddAutoMapper();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
整个对象是空的,还是仅仅是某些属性?谢谢回来-这是整个对象。我还尝试使用源和目标的显式类型,但空赋值的结果相同:CreditCardApplication cca=\u mapper.Map(applicationDetails);您肯定需要在type参数中指定目标类型。只有在映射到现有实例(例如
\u mapper.Map(source,destination)
)时,才可以使用Map
而不指定类型,因为它可以推断这两种类型。您永远不需要将这两种类型作为类型参数传递,因为它始终可以推断出至少源类型。也就是说,如果整个源对象也为null,则整个目标对象为null是有意义的唯一方法。你确定它有价值吗?是的,它肯定有价值。调试时,我可以看到NewCreditCardApplicationDetails对象进入Index方法,并且为每个属性分配了一个值。我还能够在控制器中新建一个NewCreditCardApplicationDetails对象,并尝试映射它,但我得到了相同的结果。我的映射在某个地方肯定有问题,因为作为源传递的对象肯定是正常的。再次感谢。整个对象是空的,还是只是某些属性?感谢回来-这是整个对象。我还尝试使用源和目标的显式类型,但空赋值的结果相同:CreditCardApplication cca=\u mapper.Map(applicationDetails);您肯定需要在type参数中指定目标类型。只有在映射到现有实例(例如\u mapper.Map(source,destination)
)时,才可以使用Map
而不指定类型,因为它可以推断这两种类型。您永远不需要将这两种类型作为类型参数传递,因为它始终可以推断出至少源类型。也就是说,如果整个源对象也为null,则整个目标对象为null是有意义的唯一方法。你确定它有价值吗?是的,它肯定有价值。调试时,我可以看到NewCreditCardApplicationDetails对象进入Index方法,并且为每个属性分配了一个值。我还能够在控制器中新建一个NewCreditCardApplicationDetails对象,并尝试映射它,但我得到了相同的结果。我的映射在某个地方肯定有问题,因为作为源传递的对象肯定是正常的。再次感谢。
public class CreditCardApplicationProfile : Profile
{
public CreditCardApplicationProfile()
{
CreateMap<NewCreditCardApplicationDetails, CreditCardApplication>();
}
}
public class ApplyController : Controller
{
private readonly ICreditCardApplicationRepository _applicationRepository;
private readonly IMapper _mapper;
public ApplyController(
ICreditCardApplicationRepository applicationRepository,
IMapper mapper
)
{
_applicationRepository = applicationRepository;
_mapper = mapper;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index(NewCreditCardApplicationDetails applicationDetails)
{
if (!ModelState.IsValid)
{
return View(applicationDetails);
}
CreditCardApplication cca = _mapper.Map<CreditCardApplication>(applicationDetails);
await _applicationRepository.AddAsync(cca);
return View("ApplicationComplete", cca);
}
public IActionResult Error()
{
return View();
}
}
The type arguments for method 'IMapper.Map<TDestination>(object)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
public class CreditCardApplication
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public decimal GrossAnnualIncome { get; set; }
public string FrequentFlyerNumber { get; set; }
}
public class NewCreditCardApplicationDetails
{
[Display(Name = "First Name")]
[Required(ErrorMessage = "Please provide a first name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(ErrorMessage = "Please provide a last name")]
public string LastName{ get; set; }
[Display(Name = "Age (in years)")]
[Required(ErrorMessage = "Please provide an age in years")]
[Range(18,int.MaxValue, ErrorMessage = "You must be at least 18 years old")]
public int? Age { get; set; }
[Display(Name = "Gross Income")]
[Required(ErrorMessage = "Please provide your gross income")]
public decimal? GrossAnnualIncome { get; set; }
public string FrequentFlyerNumber { get; set; }
}