Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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

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,我是一个新的AutoMapper工具,顺便说一句,这是惊人的迄今为止。我在映射模型内的集合和相应的ViewModel对象时遇到了困难 为了简单起见,我对代码进行了裁剪: 型号: public class VoteQuestion { public virtual ICollection<VoteAnswerOption> VoteAnswerOptions { get; set; } } 和相应的视图模型: public class CreateVoteQu

我是一个新的AutoMapper工具,顺便说一句,这是惊人的迄今为止。我在映射模型内的集合和相应的ViewModel对象时遇到了困难

为了简单起见,我对代码进行了裁剪:

型号:

public class VoteQuestion
{        
    public virtual ICollection<VoteAnswerOption> VoteAnswerOptions { get; set; }
}
和相应的视图模型:

public class CreateVoteQuestionViewModel
{        
    public List<VoteAnswerOptionViewModel> PossibleAnswers { get; set; }
}
public class VoteAnswerOptionViewModel
{
    public string Answer { get; set; }
}
在“Startup.cs”中设置我的映射程序。尝试了一些选项后,除了映射集合之外,它对其他所有功能都有效

        Mapper.Initialize(config =>
        {          
            config.CreateMap<VoteAnswerOption, VoteAnswerOptionViewModel>().ReverseMap();

            config.CreateMap<List<VoteAnswerOptionViewModel>, ICollection<VoteAnswerOption>>().ReverseMap();

            config.CreateMap<VoteQuestion, CreateVoteQuestionViewModel>()
            .ForMember(dest => dest.PossibleAnswers, opts => opts.MapFrom(src => src.VoteAnswerOptions))
            .ForMember(dest=>dest.PossibleAnswers,opts=>opts.MapFrom(src=>Mapper.Map<ICollection<VoteAnswerOption>, List<VoteAnswerOptionViewModel>>(src.VoteAnswerOptions)))
            .ReverseMap();
        });
Mapper.Initialize(配置=>
{          
config.CreateMap().ReverseMap();
config.CreateMap().ReverseMap();
config.CreateMap()
.FormMember(dest=>dest.PossibleAnswers,opts=>opts.MapFrom(src=>src.VoteAnswerOptions))
.FormMember(dest=>dest.PossibleAnswers,opts=>opts.MapFrom(src=>Mapper.Map(src.VoteAnswerOptions)))
.ReverseMap();
});
最后,我的控制器操作中的映射:

var newQuestion = Mapper.Map<CreateVoteQuestionViewModel, VoteQuestion>(voteQuestion);
var newQuestion=Mapper.Map(voteQuestion);

我遗漏了什么?

此测试通过:请注意,只有当映射很简单且不包含任何
FormMember
调用时,才能使用
ReverseMap()

public class VoteQuestion {
    public virtual ICollection<VoteAnswerOption> VoteAnswerOptions { get; set; }
}


public class CreateVoteQuestionViewModel {
    public List<VoteAnswerOptionViewModel> PossibleAnswers { get; set; }
}


public class VoteAnswerOption {
    public string Answer { get; set; }
}


public class VoteAnswerOptionViewModel {
    public string Answer { get; set; }
}


[TestFixture]
public class SOTests {
    [Test]
    public void Test_41247396() {
        Mapper.Initialize(config => {
            config.CreateMap<VoteAnswerOption, VoteAnswerOptionViewModel>().ReverseMap();

            config.CreateMap<VoteQuestion, CreateVoteQuestionViewModel>()
                .ForMember(dest => dest.PossibleAnswers, 
                           opts => opts.MapFrom(src => src.VoteAnswerOptions));

            config.CreateMap<CreateVoteQuestionViewModel, VoteQuestion>()
                .ForMember(dest => dest.VoteAnswerOptions, 
                           opts => opts.MapFrom(src => src.PossibleAnswers));

        });

        var voteQuestion = new VoteQuestion {
            VoteAnswerOptions = new List<VoteAnswerOption> {
                new VoteAnswerOption { Answer = "Correct" }
            }
        };

        var newQuestion = Mapper.Map<VoteQuestion, CreateVoteQuestionViewModel>(voteQuestion);
        newQuestion.PossibleAnswers.Count.Should().Be(1);
        newQuestion.PossibleAnswers.Single().Answer.Should().Be("Correct");

        var vm = new CreateVoteQuestionViewModel {
            PossibleAnswers = new List<VoteAnswerOptionViewModel> {
                new VoteAnswerOptionViewModel {Answer = "Spot on"}
            }
        };

        var q = Mapper.Map<CreateVoteQuestionViewModel, VoteQuestion>(vm);
        q.VoteAnswerOptions.Count.Should().Be(1);
        q.VoteAnswerOptions.Single().Answer.Should().Be("Spot on");

    }
}
公共类VoteQuestion{
公共虚拟ICollection VoteAnswerOptions{get;set;}
}
公共类CreateVoteQuestionViewModel{
公共列表可能的应答{get;set;}
}
公共类VoteAnswerOption{
公共字符串答案{get;set;}
}
公共类VoteAnswerOptionViewModel{
公共字符串答案{get;set;}
}
[测试夹具]
公共类考试{
[测试]
公共无效测试41247396(){
初始化(配置=>{
config.CreateMap().ReverseMap();
config.CreateMap()
.ForMember(dest=>dest.PossibleAnswers,
opts=>opts.MapFrom(src=>src.VoteAnswerOptions));
config.CreateMap()
.ForMember(dest=>dest.VoteAnswerOptions,
opts=>opts.MapFrom(src=>src.PossibleAnswers));
});
var voteQuestion=新voteQuestion{
VoteAnswerOptions=新列表{
新建VoteAnswerOption{Answer=“Correct”}
}
};
var newQuestion=Mapper.Map(voteQuestion);
newQuestion.PossibleAnswers.Count.Should()为(1);
newQuestion.PossibleAnswers.Single().Answer.Should().Be(“正确”);
var vm=新的CreateVoteQuestionViewModel{
可能的答案=新列表{
新VoteAnswerOptionViewModel{Answer=“现场”}
}
};
var q=Mapper.Map(vm);
q、 VoteAnswerOptions.Count.Should()为(1);
q、 VoteAnswerOptions.Single().Answer.Should().Be(“现场”);
}
}

这就是我的想法,我尝试了两个版本,但都不适用于我。令人惊讶的是,这个AutoMapper是一个如此酷的工具;您提供了非常好的解决方案和有用的提示;我理解我做错了什么,并解决了这个问题。谢谢你的帮助。AutoMapper真的很棒。
public class VoteQuestion {
    public virtual ICollection<VoteAnswerOption> VoteAnswerOptions { get; set; }
}


public class CreateVoteQuestionViewModel {
    public List<VoteAnswerOptionViewModel> PossibleAnswers { get; set; }
}


public class VoteAnswerOption {
    public string Answer { get; set; }
}


public class VoteAnswerOptionViewModel {
    public string Answer { get; set; }
}


[TestFixture]
public class SOTests {
    [Test]
    public void Test_41247396() {
        Mapper.Initialize(config => {
            config.CreateMap<VoteAnswerOption, VoteAnswerOptionViewModel>().ReverseMap();

            config.CreateMap<VoteQuestion, CreateVoteQuestionViewModel>()
                .ForMember(dest => dest.PossibleAnswers, 
                           opts => opts.MapFrom(src => src.VoteAnswerOptions));

            config.CreateMap<CreateVoteQuestionViewModel, VoteQuestion>()
                .ForMember(dest => dest.VoteAnswerOptions, 
                           opts => opts.MapFrom(src => src.PossibleAnswers));

        });

        var voteQuestion = new VoteQuestion {
            VoteAnswerOptions = new List<VoteAnswerOption> {
                new VoteAnswerOption { Answer = "Correct" }
            }
        };

        var newQuestion = Mapper.Map<VoteQuestion, CreateVoteQuestionViewModel>(voteQuestion);
        newQuestion.PossibleAnswers.Count.Should().Be(1);
        newQuestion.PossibleAnswers.Single().Answer.Should().Be("Correct");

        var vm = new CreateVoteQuestionViewModel {
            PossibleAnswers = new List<VoteAnswerOptionViewModel> {
                new VoteAnswerOptionViewModel {Answer = "Spot on"}
            }
        };

        var q = Mapper.Map<CreateVoteQuestionViewModel, VoteQuestion>(vm);
        q.VoteAnswerOptions.Count.Should().Be(1);
        q.VoteAnswerOptions.Single().Answer.Should().Be("Spot on");

    }
}