gpt4 book ai didi

c# - 自动映射器配置解决问题,尝试访问方法失败

转载 作者:行者123 更新时间:2023-12-03 10:32:31 32 4
gpt4 key购买 nike

我正在尝试将域模型映射到我的MVC MVVM应用程序中的viewModels。这是我的自动映射器配置的代码

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

映射配置文件:
public class BookStoreMappingProfile: Profile
{
public static MapperConfiguration InitializeAutoMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Book, BookListViewModel>();
});

return config;
}
}

Global.asax
    //AutoMapper Configuration
AutoMapperConfiguration.Configure();

我的 Controller 代码是:
    public ActionResult Index()
{
IEnumerable<Book> Books = _bookService.GetAllBooks().ToList();

IEnumerable<BookListViewModel> BookListViewModel = Mapper.Map<IEnumerable<Book>, IEnumerable<BookListViewModel>>(Books);
return View(BookListViewModel);
}

BookListViewModel.cs
public class BookListViewModel
{
[DisplayName("Name")]
public string Name { get; set; }
[DisplayName("ISBN")]
public string ISBN { get; set; }
[DisplayName("Price")]
public double Price { get; set; }
[DisplayName("Rating")]
public double Rating { get; set; }
[DisplayName("Publisher")]
public string Publisher { get; set; }
}

Book.cs
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string ISBN { get; set; }
public double Price { get; set; }
public double Rating { get; set; }
public string Publisher { get; set; }
public DateTime YearPublished { get; set; }
public string Edition { get; set; }
public int CategoryId { get; set; }
public virtual Category Categories { get; set;}
}

当我尝试在 Controller 中获取Index方法时,抛出此异常:

Attempt by method 'AutoMapper.MapperConfiguration.FindClosedGenericTypeMapFor(AutoMapper.TypePair, AutoMapper.TypePair)' to access method 'AutoMapper.MapperConfiguration+<>c__DisplayClass69_0.b__0(AutoMapper.ProfileMap)' failed.



这是方法访问​​异常,但我不知道如何解决该问题。有人可以让我洞悉那里发生的事情吗?

最佳答案

我也遇到同样的问题,并且正在使用.NET 4.5。

经过一番搜索,发现和发现错误之后,我发现问题出在System.ValueTuple上,因此,我降级到了Automapper 6.22(它不依赖于System.ValueTuple),现在我可以对其进行配置,并且一切正常美好的。

关于c# - 自动映射器配置解决问题,尝试访问方法失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52139549/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com