簡體   English   中英

Automapper 9 配置

[英]Automapper 9 Configuration

在以前版本的 AutoMapper 中,我曾經能夠像這樣配置 AutoMapper:

public static class AutoMapperFactory
{
    public static IConfigurationProvider CreateMapperConfiguration()
    {
        var config = new MapperConfiguration(cfg =>
        {
            //Scan *.UI assembly for AutoMapper Profiles
            var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));

            cfg.AddProfiles(assembly);

            cfg.IgnoreAllUnmapped();
        });

        return config;
    }
}

現在,顯示cfg.AddProfiles(assembly)的行給了我錯誤: Argument 1: cannot convert from 'System.Reflection.Assembly' to 'System.Collections.Generic.IEnumerable<AutoMapper.Profile>

如何讓IEnumerable<AutoMapper.Profile>作為參數傳遞給AddProfiles

您可以使用 addMaps,而不是 addProfile,如下所示:

public static class AutoMapperFactory
{
    public static IConfigurationProvider CreateMapperConfiguration()
    {
        var config = new MapperConfiguration(cfg =>
        {
            //Scan *.UI assembly for AutoMapper Profiles
            var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));

            cfg.AddMaps(assembly);

            cfg.IgnoreAllUnmapped();
        });

        return config;
    }
}

如文檔中所述:

配置文件內的配置僅適用於配置文件內的地圖。 應用於根配置的配置適用於創建的所有映射。

並且可以創建為具有特定類型映射的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM