简体   繁体   English

在实体框架中使用AutoMapper的异常

[英]Exception using AutoMapper with Entity Framework

I am trying to include Automapper into project using Entity Framework, this is my DTO class: 我正在尝试使用Entity Framework将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; }
}

And domain class with code first: 并首先使用代码的域类:

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; }
}

Automapper configuration: 自动映射器配置:

public static class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(config => config.AddProfile<FunctionProfile>());
    }
}

public class FunctionProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Function, FunctionDto>()
        .ForMember(dto => dto.Id, opt => opt.MapFrom(src => src.Id))
        .ForMember(dto => dto.Name, opt => opt.MapFrom(src => src.Name))
        .ForMember(dto => dto.Comment, opt => opt.MapFrom(src => src.Comment))
        .ForMember(dto => dto.StartDate, opt => opt.MapFrom(src => src.StartDate))
        .ForMember(dto => dto.EndDate, opt => opt.MapFrom(src => src.EndDate))
        .ForMember(dto => dto.ExaminationDate, opt => opt.MapFrom(src => src.ExaminationDate))
        .ForMember(dto => dto.Place, opt => opt.MapFrom(src => src.Place));
    }   
}

Then use in WebApi: 然后在WebApi中使用:

var functionDtos = functions
            .AsQueryable()
            .OrderBy(sort)
            .Skip(start)
            .Take(count)
            .ToList()
            .Select(Mapper.Map<FunctionDto>);

Of course I have register in Global: 我当然已经在Global注册了:

 AutoMapperConfiguration.Configure();

But I got the exception: 但是我有一个例外:

Missing type map configuration or unsupported mapping 缺少类型映射配置或不支持的映射

What is wrong with the code above? 上面的代码有什么问题?

Can you please confirm what functions is as the following passes: 您能否通过以下步骤确认其functions

MapperConfiguration.cs MapperConfiguration.cs

namespace StackOverflow.Function
{
    using AutoMapper;

    public class MyProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<Function, FunctionDto>();
        }
    }
}

MappingTests.cs MappingTests.cs

[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));
    }
}

Try this Configure() method, 试试这个Configure()方法,

Note: If the Function, FunctionDto has same name properties you do not need to map. 注意:如果Function, FunctionDto具有相同的名称属性,则无需映射。 AutoMapper will take care mapping. AutoMapper将注意映射。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 映射包含导航属性的实体框架实体时,Automapper异常 - Automapper Exception when mapping Entity Framework entity that contains a navigation property 当我尝试使用带有AutoMapper的Entity Framework Core更新图时出现异常 - Exception when I try to update graph using Entity Framework Core with AutoMapper 在ASP.NET MVC中将AutoMapper QueryableExtensions与Entity Framework一起使用会导致“操作可能破坏运行时的稳定性”异常 - Using AutoMapper QueryableExtensions with Entity Framework in ASP.NET MVC causes an “Operation could destabilize the runtime” exception 使用存储库模式与Entity Framework一起使用的自动映像? - Automapper for use with Entity Framework using repository pattern? 如何使用自动映射器更新实体框架中的数据? - How to update data in entity framework using automapper? AutoMapper,实体框架和计数 - AutoMapper, Entity Framework and Counts 使用Entity Framework 6的DBConcurrency异常 - DBConcurrency exception using Entity Framework 6 如何使用 Automapper 更新实体框架中的主详细信息 - How to update master detail in Entity Framework using Automapper 如何在使用空间类型和自动化时优化实体框架查询? - How to optimize Entity Framework query when using spatial types and automapper? 使用 AutoMapper 无法 Map 复杂实体框架核心 - Unable to Map Complex Entity Framework Core using AutoMapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM