C# 使用实体框架的AutoMapper时出现异常

C# 使用实体框架的AutoMapper时出现异常,c#,entity-framework,automapper,entity-framework-5,C#,Entity Framework,Automapper,Entity Framework 5,我正在尝试使用实体框架将Automapper包含到项目中,这是我的DTO类: public class FunctionDto { public int Id { get; set; } public string Name { get; set; } public DateTime? StartDate { get; set; } public DateTime? EndDate { get; set; } public string Comment {

我正在尝试使用实体框架将Automapper包含到项目中,这是我的DTO类:

public class FunctionDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string Comment { get; set; }
    public DateTime? ExaminationDate { get; set; }
    public string Place { get; set; }
}
和域类,首先使用代码:

public class Function
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string Comment { get; set; }
    public DateTime? ExaminationDate { get; set; }
    public string Place { get; set; }

    public virtual List<Employee> Employees { get; set; }
}
但我有个例外:

缺少类型映射配置或不支持的映射


上面的代码有什么问题吗?

请确认以下过程中的功能:

MapperConfiguration.cs

MappingTests.cs

试试这个配置方法

注意:如果函数、FunctionDto具有相同的名称属性,则不需要映射。AutoMapper将负责映射

protected override void Configure()
{
    CreateMap<Function, FunctionDto>().IgnoreAllNonExisting();
}
-


目前还没有答案,但这就足够做CreateMap了;因为所有成员都有相同的名字。如果将初始化语句放在linq查询之前会发生什么?只是为了trying@GertArnold:Dto没有员工,只是要明确。我尝试了functions.SelectMapper.Map,但仍然得到相同的错误。由于延迟加载,这里的函数也是代理类。我使用AutoMapper 2.1 Nuget,它从代理映射到dto的v.v。我知道代理可能会导致问题,在这里有一些问题,但在我使用的版本中似乎没有问题。
var functionDtos = functions
            .AsQueryable()
            .OrderBy(sort)
            .Skip(start)
            .Take(count)
            .ToList()
            .Select(Mapper.Map<FunctionDto>);
 AutoMapperConfiguration.Configure();
namespace StackOverflow.Function
{
    using AutoMapper;

    public class MyProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<Function, FunctionDto>();
        }
    }
}
[TestFixture]
public class MappingTests
{
    [Test]
    public void AutoMapper_Configuration_IsValid()
    {
        Mapper.Initialize(m => m.AddProfile<MyProfile>());
        Mapper.AssertConfigurationIsValid();
    }

    [Test]
    public void AutoMapper_Mapping_IsValid()
    {
        Mapper.Initialize(m => m.AddProfile<MyProfile>());
        Mapper.AssertConfigurationIsValid();

        var functions = new List<Function>
            {
                new Function
                    {
                        Comment = "Stack Overflow Rocks",
                        EndDate = new DateTime(2012, 01, 01),
                        ExaminationDate = new DateTime(2012, 02, 02),
                        Id = 1,
                        Name = "Number 1",
                        Place = "Here, there and everywhere",
                        StartDate = new DateTime(2012, 03, 03)
                    },
                new Function
                    {
                        Comment = "As do I",
                        EndDate = new DateTime(2013, 01, 01),
                        ExaminationDate = new DateTime(2013, 02, 02),
                        Id = 2,
                        Name = "Number 2",
                        Place = "Nowhere",
                        StartDate = new DateTime(2013, 03, 03)
                    }
            };

        var functionDtos = functions
            .AsQueryable()
            .OrderBy(x => x.Id)
            .Skip(1)
            .Take(1)
            .ToList()
            .Select(Mapper.Map<FunctionDto>);

        Assert.That(functionDtos, Is.Not.Null);
        Assert.That(functionDtos.Count(), Is.EqualTo(1));
        Assert.That(functionDtos.First().Id, Is.EqualTo(2));
    }
}
protected override void Configure()
{
    CreateMap<Function, FunctionDto>().IgnoreAllNonExisting();
}
    public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
    {
        var sourceType = typeof(TSource);
        var destinationType = typeof(TDestination);
        var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
        foreach (var property in existingMaps.GetUnmappedPropertyNames())
        {
            expression.ForMember(property, opt => opt.Ignore());
        }
        return expression;
    }