映射器不支持的映射
Mapper unsupported mapping
我在使用 Automapper 时遇到困难,希望您能提供帮助。我正在尝试从 DailyCourses 映射到 DailyCoursesSearch,但我一直遇到异常。我知道映射存在一些问题,但我已尝试切换对象位置,确保所有 tzpe 都是相同的 treid 以从成员映射到成员但没有成功。它确实适用于另一个 类.
这是我的错误
Missing type map configuration or unsupported mapping.
Mapping types:
Object -> DailyCourseSearch
System.Object -> Core.Contracts.Models.DailyCourseSearch
这些都是类提到的
public class DailyCourses
{
public DateTimeOffset? CourseDate { get; set; }
public IEnumerable<DailyCourse> Items { get; set; }
}
public class DailyCoursesSearch
{
public DateTimeOffset? CourseDate { get; set; }
public IEnumerable<DailyCourseSearch> Items { get; set; }
}
public class DailyCourseSearch
{
public string CountryName { get; set; } = null!;
public string CurrencyName { get; set; } = null!;
public string Amount { get; set; }
public string CountryCode { get; set; } = null!;
public string Course { get; set; }
}
public class DailyCourse
{
[Required]
public string CountryName { get; set; } = null!;
[Required]
public string CurrencyName { get; set; } = null!;
//TODO to short
[Required]
public string Amount { get; set; }
/// <summary>
/// ISO 3-letter code
/// </summary>
[Required]
public string CountryCode { get; set; } = null!;
//TODO to double
[Required]
public string Course { get; set; }
}
public class MapperTest
{
[Test]
public void Mapping()
{
var dailyCourses = new DailyCourses();
dailyCourses.Items = new List<DailyCourse>()
{
new DailyCourse()
{
Amount = "TEst"
}
};
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DailyCourses, DailyCoursesSearch>().ForMember(a => a.Items, o => o.MapFrom(y => y.Items));
cfg.CreateMap<DailyCourse, DailyCourseSearch>();
});
var mapper = new Mapper(config);
mapper.Map<DailyCourseSearch>(dailyCourses);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup).Assembly);
}
您的代码中存在一个很难发现的错误,并且刚刚在编写完整的 running example
时发现了它
using System;
using System.Collections.Generic;
using System.Text.Json;
using AutoMapper;
public static class Program
{
public static void Main()
{
var dailyCourses = new DailyCourses();
dailyCourses.Items = new List<DailyCourse>()
{
new DailyCourse { Amount = "Test" }
};
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DailyCourse, DailyCourseSearch>();
cfg.CreateMap<DailyCourses, DailyCoursesSearch>()
.ForMember(a => a.Items, o => o.MapFrom(y => y.Items));
});
var mapper = new Mapper(config);
var search = mapper.Map<DailyCoursesSearch>(dailyCourses);
var json = JsonSerializer.Serialize(search);
Console.WriteLine(json);
}
}
public class DailyCourses
{
public DateTimeOffset? CurseDate { get; set; }
public IEnumerable<DailyCourse> Items { get; set; }
}
public class DailyCoursesSearch
{
public DateTimeOffset? CurseDate { get; set; }
public IEnumerable<DailyCourseSearch> Items { get; set; }
}
public class DailyCourseSearch
{
public string CountryName { get; set; }
public string CurrencyName { get; set; }
public string Amount { get; set; }
public string CountryCode { get; set; }
public string Course { get; set; }
}
public class DailyCourse
{
public string CountryName { get; set; }
public string CurrencyName { get; set; }
public string Amount { get; set; }
public string CountryCode { get; set; }
public string Course { get; set; }
}
您的代码中的错误是这一行:
mapper.Map<DailyCourseSearch>(dailyCourses);
应该是这段代码可以工作:
mapper.Map<DailyCoursesSearch>(dailyCourses);
所以原因是你的代码中有一个简单的拼写错误,应该关闭这个问题。
我在使用 Automapper 时遇到困难,希望您能提供帮助。我正在尝试从 DailyCourses 映射到 DailyCoursesSearch,但我一直遇到异常。我知道映射存在一些问题,但我已尝试切换对象位置,确保所有 tzpe 都是相同的 treid 以从成员映射到成员但没有成功。它确实适用于另一个 类.
这是我的错误
Missing type map configuration or unsupported mapping.
Mapping types:
Object -> DailyCourseSearch
System.Object -> Core.Contracts.Models.DailyCourseSearch
这些都是类提到的
public class DailyCourses
{
public DateTimeOffset? CourseDate { get; set; }
public IEnumerable<DailyCourse> Items { get; set; }
}
public class DailyCoursesSearch
{
public DateTimeOffset? CourseDate { get; set; }
public IEnumerable<DailyCourseSearch> Items { get; set; }
}
public class DailyCourseSearch
{
public string CountryName { get; set; } = null!;
public string CurrencyName { get; set; } = null!;
public string Amount { get; set; }
public string CountryCode { get; set; } = null!;
public string Course { get; set; }
}
public class DailyCourse
{
[Required]
public string CountryName { get; set; } = null!;
[Required]
public string CurrencyName { get; set; } = null!;
//TODO to short
[Required]
public string Amount { get; set; }
/// <summary>
/// ISO 3-letter code
/// </summary>
[Required]
public string CountryCode { get; set; } = null!;
//TODO to double
[Required]
public string Course { get; set; }
}
public class MapperTest
{
[Test]
public void Mapping()
{
var dailyCourses = new DailyCourses();
dailyCourses.Items = new List<DailyCourse>()
{
new DailyCourse()
{
Amount = "TEst"
}
};
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DailyCourses, DailyCoursesSearch>().ForMember(a => a.Items, o => o.MapFrom(y => y.Items));
cfg.CreateMap<DailyCourse, DailyCourseSearch>();
});
var mapper = new Mapper(config);
mapper.Map<DailyCourseSearch>(dailyCourses);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup).Assembly);
}
您的代码中存在一个很难发现的错误,并且刚刚在编写完整的 running example
时发现了它using System;
using System.Collections.Generic;
using System.Text.Json;
using AutoMapper;
public static class Program
{
public static void Main()
{
var dailyCourses = new DailyCourses();
dailyCourses.Items = new List<DailyCourse>()
{
new DailyCourse { Amount = "Test" }
};
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DailyCourse, DailyCourseSearch>();
cfg.CreateMap<DailyCourses, DailyCoursesSearch>()
.ForMember(a => a.Items, o => o.MapFrom(y => y.Items));
});
var mapper = new Mapper(config);
var search = mapper.Map<DailyCoursesSearch>(dailyCourses);
var json = JsonSerializer.Serialize(search);
Console.WriteLine(json);
}
}
public class DailyCourses
{
public DateTimeOffset? CurseDate { get; set; }
public IEnumerable<DailyCourse> Items { get; set; }
}
public class DailyCoursesSearch
{
public DateTimeOffset? CurseDate { get; set; }
public IEnumerable<DailyCourseSearch> Items { get; set; }
}
public class DailyCourseSearch
{
public string CountryName { get; set; }
public string CurrencyName { get; set; }
public string Amount { get; set; }
public string CountryCode { get; set; }
public string Course { get; set; }
}
public class DailyCourse
{
public string CountryName { get; set; }
public string CurrencyName { get; set; }
public string Amount { get; set; }
public string CountryCode { get; set; }
public string Course { get; set; }
}
您的代码中的错误是这一行:
mapper.Map<DailyCourseSearch>(dailyCourses);
应该是这段代码可以工作:
mapper.Map<DailyCoursesSearch>(dailyCourses);
所以原因是你的代码中有一个简单的拼写错误,应该关闭这个问题。