I have been looking at this document https://automapper.readthedocs.io/en/stable/Getting-started.html to try to configure automapper to take away the pain of some manual attribute mapping.
I am coming up against the following error:
Message: System.InvalidOperationException : Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.
This bit - "Mapper not initialized. Call Initialize with appropriate configuration" - is concerning because I feel I have done that in my startup.cs file.
I have the following controller action:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index(NewCreditCardApplicationDetails applicationDetails)
{
if (!ModelState.IsValid)
{
return View(applicationDetails);
}
CreditCardApplication creditCardApplication = Mapper.Map<CreditCardApplication>(applicationDetails);
//var creditCardApplication = new CreditCardApplication
//{
// FirstName = applicationDetails.FirstName,
// LastName = applicationDetails.LastName,
// Age = applicationDetails.Age.Value,
// GrossAnnualIncome = applicationDetails.GrossAnnualIncome.Value,
// FrequentFlyerNumber = applicationDetails.FrequentFlyerNumber
//};
await _applicationRepository.AddAsync(creditCardApplication);
return View("ApplicationComplete", creditCardApplication);
}
The commented lines represent code that did work, but I want to remove.
My configure method in startup.cs file in my asp.net core web app looks like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, AppDbContext dbContext)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
dbContext.Database.EnsureCreated();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
Mapper.Initialize(cfg =>
{
cfg.CreateMap<NewCreditCardApplicationDetails, CreditCardApplication>();
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
NewCreditCardApplicationDetails.cs looks like this:
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; }
}
CreditCardApplication.cs looks like this:
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; }
}
Can anyone see what the issue is please?
Thanks.