Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射导航属性问题_C#_Asp.net Mvc_Automapper - Fatal编程技术网

C# 自动映射导航属性问题

C# 自动映射导航属性问题,c#,asp.net-mvc,automapper,C#,Asp.net Mvc,Automapper,我正在遵循这个答案,并将其与我的CategoryId绑定到UploadSongViewModel上的SelectedCategoryIds,但是我还想让它将SongId绑定到UploadSongViewModel上的Id namespace App { public static class AutoMapper { public static MapperConfiguration Config; public static void Init

我正在遵循这个答案,并将其与我的
CategoryId
绑定到
UploadSongViewModel
上的
SelectedCategoryIds
,但是我还想让它将
SongId
绑定到
UploadSongViewModel
上的
Id

namespace App
{
    public static class AutoMapper
    {
        public static MapperConfiguration Config;

        public static void Initialize()
        {
            //It works with CategoryId, but throws an error when I include SondId.
            Config = new MapperConfiguration(x =>
            {
                x.CreateMap<int, SongCategory>()
                    .IgnoreAllNonExisting()
                    .ForMember(dest => dest.CategoryId, opt => opt.MapFrom(src => src))
                    .ForMember(dest => dest.SongId, opt => opt.MapFrom(src => src)); //Code that's throwing exception

                x.CreateMap<UploadSongViewModel, Song>()
                    .IgnoreAllNonExisting()
                    .ForMember(dest => dest.AudioName, opt => opt.MapFrom(src => src.SongName))
                    .ForMember(dest => dest.AudioPath, opt => opt.MapFrom(src => src.SongPath))
                    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.SelectedCategoryIds))
                    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.Id)); //Code that's throwing exception
            });

            //Throws exception here because it can't bind SongId property
            Config.AssertConfigurationIsValid(); 
        }
    }
}

public abstract class Entity
{
    public int Id { get; set; }
}

public class SongCategory : Entity
{
    public int SongId { get; set; }
    public int CategoryId { get; set; }
}

public class Song : Audio
{
    public string AlbumName { get; set; }
    public string ArtistName { get; set; }

    public List<SongCategory> SongCategories { get; set;}
}

public class UploadSongViewModel
{
    public int Id { get; set; }
    public string ArtistName { get; set; }
    public string AlbumName { get; set; }
    public int[] SelectedCategoryIds { get; set; }

    public MultiSelectList CategoriesSelectList { get; set; }
}
命名空间应用程序
{
公共静态类自动映射器
{
公共静态MapperConfiguration配置;
公共静态void Initialize()
{
//它与CategoryId一起工作,但当我包含SondId时会抛出一个错误。
配置=新的MapperConfiguration(x=>
{
x、 CreateMap()
.ignoreall不存在()
.FormMember(dest=>dest.CategoryId,opt=>opt.MapFrom(src=>src))
.ForMember(dest=>dest.SongId,opt=>opt.MapFrom(src=>src));//引发异常的代码
x、 CreateMap()
.ignoreall不存在()
.FormMember(dest=>dest.AudioName,opt=>opt.MapFrom(src=>src.SongName))
.ForMember(dest=>dest.AudioPath,opt=>opt.MapFrom(src=>src.SongPath))
.FormMember(dest=>dest.SongCategories,opt=>opt.MapFrom(src=>src.selectedCategorids))
.ForMember(dest=>dest.songcomposes,opt=>opt.MapFrom(src=>src.Id));//引发异常的代码
});
//在此处引发异常,因为它无法绑定SongId属性
Config.assertconfigurationsvalid();
}
}
}
公共抽象类实体
{
公共int Id{get;set;}
}
公共类类别:实体
{
public int SongId{get;set;}
public int CategoryId{get;set;}
}
公共课歌曲:音频
{
公共字符串名称{get;set;}
公共字符串ArtistName{get;set;}
公共列表类别{get;set;}
}
公共类上载模型
{
公共int Id{get;set;}
公共字符串ArtistName{get;set;}
公共字符串名称{get;set;}
public int[]selectedCategoryId{get;set;}
公共多选列表分类选择列表{get;set;}
}
我不太明白Darin Dimitrov answer的自动映射程序代码在做什么,所以很难调试。如果有人能解释一下categoryId的映射是如何工作的,以及为什么SongId不能工作,那就太好了

我得到的例外是:

无法映射System.Collections.Generic.List
1[[SoundVast.Models.SongCategory,SoundVast,Version=1.0.0,Culture=neutral,PublicKeyToken=null]]上的以下属性:
歌曲类别
添加自定义映射表达式、忽略、添加自定义解析程序或修改目标类型System.Collections.Generic.List
1[[SoundVast.Models.SongCategory,SoundVast,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]。 背景: 从System.Int32映射到属性SongCategories到System.Collections.Generic.List`1[[SoundVast.Models.SongCategory,SoundVast,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]] 从类型SoundVast.Areas.Upload.Models.UploadSongViewModel映射到SoundVast.Models.Song 引发了类型为“AutoMapper.AutoMapperConfigurationException”的异常


一旦应用了基本规则,就可以通过一些自定义映射轻松实现这一点:

x.CreateMap<int, SongCategory>()
    .IgnoreAllNonExisting()
    .ForMember(dest => dest.CategoryId, opt => opt.MapFrom(src => src));

x.CreateMap<UploadSongViewModel, Song>()
    .IgnoreAllNonExisting()
    .ForMember(dest => dest.AudioName, opt => opt.MapFrom(src => src.SongName))
    .ForMember(dest => dest.AudioPath, opt => opt.MapFrom(src => src.SongPath))
    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.SelectedCategoryIds))
    .AfterMap((viewModel, model) => 
    {
        foreach (var item in model.SongCategories)
        {
            item.Id = viewModel.Id;
        }
    });
x.CreateMap()
.ignoreall不存在()
.ForMember(dest=>dest.CategoryId,opt=>opt.MapFrom(src=>src));
x、 CreateMap()
.ignoreall不存在()
.FormMember(dest=>dest.AudioName,opt=>opt.MapFrom(src=>src.SongName))
.ForMember(dest=>dest.AudioPath,opt=>opt.MapFrom(src=>src.SongPath))
.FormMember(dest=>dest.SongCategories,opt=>opt.MapFrom(src=>src.selectedCategorids))
.AfterMap((视图模型,模型)=>
{
foreach(model.SongCategories中的var项)
{
item.Id=viewModel.Id;
}
});

一旦应用了基本规则,就可以通过一些自定义映射轻松实现这一点:

x.CreateMap<int, SongCategory>()
    .IgnoreAllNonExisting()
    .ForMember(dest => dest.CategoryId, opt => opt.MapFrom(src => src));

x.CreateMap<UploadSongViewModel, Song>()
    .IgnoreAllNonExisting()
    .ForMember(dest => dest.AudioName, opt => opt.MapFrom(src => src.SongName))
    .ForMember(dest => dest.AudioPath, opt => opt.MapFrom(src => src.SongPath))
    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.SelectedCategoryIds))
    .AfterMap((viewModel, model) => 
    {
        foreach (var item in model.SongCategories)
        {
            item.Id = viewModel.Id;
        }
    });
x.CreateMap()
.ignoreall不存在()
.ForMember(dest=>dest.CategoryId,opt=>opt.MapFrom(src=>src));
x、 CreateMap()
.ignoreall不存在()
.FormMember(dest=>dest.AudioName,opt=>opt.MapFrom(src=>src.SongName))
.ForMember(dest=>dest.AudioPath,opt=>opt.MapFrom(src=>src.SongPath))
.FormMember(dest=>dest.SongCategories,opt=>opt.MapFrom(src=>src.selectedCategorids))
.AfterMap((视图模型,模型)=>
{
foreach(model.SongCategories中的var项)
{
item.Id=viewModel.Id;
}
});