2012-11-29 218 views
1

我想包括Automapper到使用实体框架的项目,这是我的DTO类:异常使用AutoMapper与实体框架

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

Automapper配置:

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

然后在WebApi中使用:

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

当然,我有登记在全球:

AutoMapperConfiguration.Configure(); 

但我得到异常:

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

是什么上面的代码错误?

+1

不是一个答案(还),但它会做够'CreateMap <功能,FunctionDto>();',因为所有成员具有相同的名称。如果将初始化语句放在linq查询之前会发生什么? (只是为了尝试) –

+0

@GertArnold:Dto没有员工,只是明确。我尝试了'功能。选择(Mapper.Map )',仍然得到相同的错误。此外功能在这里是代理类,因为延迟加载 –

+0

我使用AutoMapper 2.1(Nuget),它从代理映射到dto的v.v.我知道代理可能会导致问题(在这里有问题),但在我使用的版本中似乎没问题。 –

回答

0

能否请你证实了functions是以下遍:

MapperConfiguration.cs

namespace StackOverflow.Function 
{ 
    using AutoMapper; 

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

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

试试这个方法Configure()

注意:如果Function, FunctionDto具有相同的名称属性,则不需要映射。 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; 
    } 
相关问题