当地图存在时,AutoMapper会丢失地图 [英] AutoMapper throws Missing Map when Map exists

查看:121
本文介绍了当地图存在时,AutoMapper会丢失地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

规格: .NET 4.5.1 - MVC 5.2.2 - EF 6.0 - AutoMapper 3.2.1



对象错误,但是可以通过执行以下操作来解决它: AutoMapper 3.1.1和实体框架6.1代理对象



一旦我修复了这个错误,我马上得到以下错误信息:



由于某些原因, PagesViewModel的页面不存在,即使这样。以下是我的代码:



Global.asax.cs 中:

  protected void Application_Start()
{
ConfigureAutomapper.Configure();
....

AutoMapper.cs(UPDATED)

  public static class ConfigureAutomapper 
{
public static void Configure()
{
ConfigurePublications();
ConfigureBlog();
ConfigureBasic();
ConfigureLists();
}

public static void ConfigurePublications()
{
Mapper.Initialize(x =>
{
x.AddProfile< PublicationsMappings> ;();
});
}

public static void ConfigureBlog()
{
Mapper.Initialize(x =>
{
x.AddProfile< BlogMappings> ;();
});
}

public static void ConfigureBasic()
{
Mapper.Initialize(x =>
{
x.AddProfile< BasicMappings> ;();
});
}

public static void ConfigureLists()
{
Mapper.Initialize(x =>
{
x.AddProfile< ListMappings> ;();
});
}
}

...

  public class BasicMappings:Profile 
{
public override string ProfileName
{
get
{
返回BasicMappings;
}
}

protected override void Configure()
{
Mapper.CreateMap< Pages,PagesViewModel>();

Mapper.CreateMap< PagesViewModel,Pages>();
...

我已经跟踪它,它会在那里创建地图。当它使用转换时,它不起作用。



对于model和viewModel,所有变量名都相同。我只添加了数据注释和一些消毒类 set {pageDescription = value.StringSanitizer(); } / code $



区别在于声明链接对象/表:



型号:

  public virtual PageType PageTypes {get;组; } 
public virtual SiteMap SiteMap {get;组; }
public virtual ICollection< Rows>行{get;组; }
public virtual Track Tracks {get;组;

而我的 ViewModel:

  public PageTypeViewModel PageTypes {get;组; } 
public SiteMapViewModel SiteMap {get;组; }
public ICollection< RowsViewModel>行{get;组; }
public TrackViewModel Tracks {get;组; }

连接到这些模型的viewModels。所有这些都映射到AutoMapper.cs中






更新:
我有项目中已经有一个单元测试:

  [TestMethod] 
public void AutoMapper_Basic_Configuration_IsValid()
{
//安排

// Act
ConfigureAutomapper.ConfigureBasic();

// Assert
Mapper.AssertConfigurationIsValid();
}

它通过了所有没有映射错误的测试。 >



任何人都可以在这个问题上给我一些洞察力?如果您需要更多信息,请通知我。谢谢!

解决方案

而不是 Mapper.CreateMap< Pages,PagesViewModel>(); ,使用 CreateMap< Pages,PagesViewModel>();

  protected override void Configure()
{
CreateMap< Pages,PagesViewModel>();

CreateMap< PagesViewModel,Pages>();
...

在个人资料中,您需要参考 Mapper 这是 this.CreateMap 或简单地 CreateMap



更新
更新我的答案,以便未来的读者不必通过评论来解决问题。 / p>

问题是多次调用 Mapper.Initialize 。每个新的呼叫将清除以前的初始化。修复是单个初始化调用,并添加所有配置文件。


Specifications: .NET 4.5.1 - MVC 5.2.2 - EF 6.0 - AutoMapper 3.2.1

I was first having Proxy object error but was able to solve it by doing the following: AutoMapper 3.1.1 and Entity Framework 6.1 Proxy objects

Once I fixed that error, I instantly got the following error message:

For some reason, it says the map from Pages to PagesViewModel doesn't exist, even though it does. Here is my code:

In Global.asax.cs:

protected void Application_Start()
{
     ConfigureAutomapper.Configure();
     ....

In AutoMapper.cs (UPDATED)

public static class ConfigureAutomapper
{
    public static void Configure()
    {
        ConfigurePublications();
        ConfigureBlog();
        ConfigureBasic();
        ConfigureLists();
    }

    public static void ConfigurePublications()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<PublicationsMappings>();
        });
    }

    public static void ConfigureBlog()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<BlogMappings>();
        });
    }

    public static void ConfigureBasic()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<BasicMappings>();
        });
    }

    public static void ConfigureLists()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<ListMappings>();
        });
    }
}

...

public class BasicMappings : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "BasicMappings";
            }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<Pages, PagesViewModel>();

            Mapper.CreateMap<PagesViewModel, Pages>();
            ...

I have traced it and it gets in there and creates the map. When it goes to use the conversion, it doesn't work.

For the model and viewModel, all the variable names are the same. I only added data annotations and some sanitize classes set { pageDescription = value.StringSanitizer(); }

The difference comes when declaring linked objects/tables:

Model:

public virtual PageType PageTypes { get; set; }
public virtual SiteMap SiteMap { get; set; }
public virtual ICollection<Rows> Row { get; set; }
public virtual Track Tracks { get; set; }

while my ViewModel:

public PageTypeViewModel PageTypes { get; set; }
public SiteMapViewModel SiteMap { get; set; }
public ICollection<RowsViewModel> Row { get; set; }
public TrackViewModel Tracks { get; set; }

Connects to the viewModels of those models. All of which are mapped in AutoMapper.cs


UPDATE: I have a unit test already in the project:

[TestMethod]
public void AutoMapper_Basic_Configuration_IsValid()
{
      //Arrange

      //Act
      ConfigureAutomapper.ConfigureBasic();

      //Assert
      Mapper.AssertConfigurationIsValid();
 }

It passed all the tests with no mapping errors.

Can anyone give me some insight on this issue? Please let me know if you need more information. Thank you!

解决方案

Instead of Mapper.CreateMap<Pages, PagesViewModel>();, use CreateMap<Pages, PagesViewModel>();.

protected override void Configure()
    {
        CreateMap<Pages, PagesViewModel>();

        CreateMap<PagesViewModel, Pages>();
     ...

Inside a profile, you need to refer to the instance of Mapper which is this.CreateMap or simply CreateMap.

Update Updating my answer so that future readers don't have to wade thru the comments to figure out the issue.

The problem was with multiple calls to Mapper.Initialize. Each new call would clear out previous initialization. The fix was to have a single initialization call and add all profiles in there.

这篇关于当地图存在时,AutoMapper会丢失地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆