Entity framework 自动映射随机丢失映射信息

Entity framework 自动映射随机丢失映射信息,entity-framework,wcf,mapping,automapper,Entity Framework,Wcf,Mapping,Automapper,我们有一个解决方案,使用WCF服务从EF 6数据库获取数据。存储库DLL通过IoC容器注入。在这个DLL中,您可以找到实体、上下文、自动映射配置文件等 以下是SubscriberProfile的示例: public class SubscriberProfile : Profile { protected override void Configure() { MapDbToDto(); } pri

我们有一个解决方案,使用WCF服务从EF 6数据库获取数据。存储库DLL通过IoC容器注入。在这个DLL中,您可以找到实体、上下文、自动映射配置文件等

以下是SubscriberProfile的示例:

public class SubscriberProfile : Profile
    {
        protected override void Configure()
        {
            MapDbToDto();
        }

        private static void MapDbToDto()
        {
            Mapper.CreateMap<SubscriberDb, SubscriberDto>()
                  .ForMember(x => x.FieldA, opt => opt.MapFrom(x => x.Filed123.Valeur))
                  .ForMember(x => x.Contact, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Contact)))
                  .ForMember(x => x.Conjoint, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Conjoint)));
            Mapper.CreateMap<AnotherDb, AnotherDto>();
            Mapper.CreateMap<ContactDb, ContactDto>();
        }
    }
在每个repo构造函数中,都会调用Configure方法,因此它会获得映射:

public SubscriberReadRepository(PlansContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            AutoMapperRepositoryConfiguration.Configure();
            _context = context;
        }
一切都很好,几乎一直如此。在服务器上,有时会突然出现臭名昭著的缺失类型映射配置或不受支持的映射错误:


一旦我们得到错误,随后的调用都不起作用,它们都会因相同的错误而崩溃。然后我们转到IIS并重新启动WCF服务,一切都恢复正常。它可以毫无问题地工作几天,然后又无缘无故地再次发生。我们的DBA保证未对数据库进行任何修改。有人有主意吗?我在谷歌上到处查看,但找不到像我们这样随机的东西。我在这里看到一篇帖子,其中Jimmy Bogard谈到在Mapper.CreateMap上调用base.CreateMap,但还没有尝试过,因为我们甚至不知道为什么会发生这种错误

您调用了错误的CreateMap方法。在Profile类中调用基本CreateMap方法:

public class SubscriberProfile : Profile
{
    protected override void Configure()
    {
        MapDbToDto();
    }

    private static void MapDbToDto()
    {
        CreateMap<SubscriberDb, SubscriberDto>()
              .ForMember(x => x.FieldA, opt => opt.MapFrom(x => x.Filed123.Valeur))
              .ForMember(x => x.Contact, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Contact)))
              .ForMember(x => x.Conjoint, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Conjoint)));
        CreateMap<AnotherDb, AnotherDto>();
        CreateMap<ContactDb, ContactDto>();
    }
}

您的createmap处于静态方法中。请看下面的答案:谢谢!看起来更干净。希望它能解决我们的问题。奥特曼:这解决了你的问题吗?我面临同样的问题,但我已经这样做了。我有同样的问题。这个解决方案有效吗?我遇到了同样的问题。@DavidDerman您使用的是什么版本的AutoMapper?如果您再次遇到此问题,请在AutoMapper GitHub repo中提出。@JimmyBogard-我们使用的是4.2.1。升级到6.0会解决这个问题吗?另外,有没有关于如何在asp.net应用程序中最好地实现automapper的文章?
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Missing type map configuration or unsupported mapping.

Mapping types:
SubscriberDb -> SubscriberDto
MyRepo.SouscripteurDb -> MyRepo.SubscriberDto

Destination path:
SubscriberDto

Source value:
System.Data.Entity.DynamicProxies.SubscriberDb_0498438FE3D70326924850E4199183EC0EB2AC8DF509202F8CB4EF2D02D9E835 (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
SubscriberDb -> SubscriberDto
MyRepo.SouscripteurDb -> MyRepo.SouscripteurDto

Destination path:
SubscriberDto

Source value:
System.Data.Entity.DynamicProxies.SubscriberDb_0498438FE3D70326924850E4199183EC0EB2AC8DF509202F8CB4EF2D02D9E835
   at MyRepo.SubscriberReadRepository.Get(Int32 idSouscripteur) in e:\BuildAgent\work\9dd90bc72a81e0e3\Sources\MyRepo\SubscriberReadRepository.cs:line 59
   at MyRepo.SubscriberReadServices.Get(Int32 id) in e:\BuildAgent5\work\9dd90bc72a81e0e3\Sources\MyRepo\SubscriberReadServices.cs:line 33
   at WCF.Services.SubscriberServices.Get(Int32 id) in e:\BuildAgen...).
public class SubscriberProfile : Profile
{
    protected override void Configure()
    {
        MapDbToDto();
    }

    private static void MapDbToDto()
    {
        CreateMap<SubscriberDb, SubscriberDto>()
              .ForMember(x => x.FieldA, opt => opt.MapFrom(x => x.Filed123.Valeur))
              .ForMember(x => x.Contact, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Contact)))
              .ForMember(x => x.Conjoint, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Conjoint)));
        CreateMap<AnotherDb, AnotherDto>();
        CreateMap<ContactDb, ContactDto>();
    }
}
public static class AutoMapperRepositoryConfiguration
{  
    //Create mapings only once
    public static void Configure()
    {
        if (Mapper.GetAllTypeMaps().Any())
            return;

        Mapper.Initialize(cfg => {
            cfg.DisableConstructorMapping();

            cfg.AddProfile(new ConventionProfile());
            cfg.AddProfile(new SubscriberProfile());
            cfg.AddProfile(new BeneficiaryProfile());
        });
    }
}