本文介绍了不能第二实体添加到视图模型与Automapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习MVC和我最近刚提出来看看使用AutoMapper映射视图模型我。于是,我开始了测试,但我不理解如何映射两个实体也可以找到一个例子。

我已经是一个Person表和一个地址表。我想这两个结合成一个ViewModel传递给我的看法。看着他们的例子总是显示如何映射一个实体,但不是两个。

在以下code,我可以看到我的两个currentPerson和currentAddress对象填充用数据调试器,但后来我currentMember视图模型只具有人的数据。这是有道理的,因为我只使用Mapper.Map的人,但我怎么告诉currentMember映射地址呢?

 公众的ActionResult编辑(INT ID = 0)
        {
            使用(DataContext的DB =新的DataContext())
            {
                人currentPerson = db.Person.Find(ID);
                解决currentAddress = db.Address.Single(一个= GT; a.PID == ID);                AutoMapper.Mapper.CreateMap<人AdminViewModel>();
                AutoMapper.Mapper.CreateMap<地址,AdminViewModel>();                MemberViewModel currentMember = AutoMapper.Mapper.Map< AdminViewModel>(currentPerson);                返回查看(currentMember);
            }
        }


解决方案

AutoMapper.Map 的过载,需要一个源和目标对象。所以,你可以使用像这样利用它的优势:

创建的ViewModels

 公共类PersonViewModel
{
    //此处是您的人..领域
    //使用相同的名称作为人创建他们
    //以避免映射期间设置每个字段
}公共类AddressViewModel
{
    //此处是您的地址字段..
}公共类AdminViewModel
{
    公共PersonViewModel人{搞定;组;}    公共AddressViewModel地址{搞定;组;}
}

克里特岛的映射(你应该集中在一个类中所有的映射):

 公共类EntityToViewModelProfile:简介
{
    保护覆盖无效配置()
    {
        //创建Person和地址映射
        AutoMapper.Mapper.CreateMap<人AdminViewModel>();
        AutoMapper.Mapper.CreateMap<地址,AdminViewModel>();
        //创建一个映射到AdminViewModel为Person和Address        Mapper.CreateMap< Models.Person,Models.AdminViewModel>()
            .ForMember(DEST => dest.Person,选择=> opt.MapFrom(SRC => SRC));        Mapper.CreateMap< Models.Address,Models.AdminViewModel>()
            .ForMember(DEST => dest.Address,选择=> opt.MapFrom(SRC => SRC));
    }
}

的Global.asax.cs

注册EntityToViewModelProfile

  AutoMapper.Mapper.Initialize(X =>
{
    x.AddProfile< EntityToViewModelProfile>();
});

然后,在你的控制器使用 .MAP

过载

 使用(DataContext的DB =新的DataContext())
{
    人currentPerson = db.Person.Find(ID);
    解决currentAddress = db.Address.Single(一个= GT; a.PID == ID);    VAR mapPerson = AutoMapper.Mapper.Map<人AdminViewModel>(currentPerson);    VAR mapPersonAndAddress = AutoMapper.Mapper.Map<地址,AdminViewModel>(currentAddress,mapPerson);    返回查看(mapPersonAndAddress);
}

I'm still learning MVC and I'm was just recently suggested to look at using AutoMapper to map my ViewModel. So, I started a test but I'm not understanding how to map two entities nor can I find an example.

What I have is a Person table and an Address table. I want to combine the two into a ViewModel to pass to my View. Looking at examples they always show how to map one entity but not two.

In the following code I can see that both my currentPerson and currentAddress objects are populated with data using the debugger but then my currentMember ViewModel only has the Person data. That makes sense because I'm only using Mapper.Map on the Person but how do I tell the currentMember to map the Address as well?

public ActionResult Edit(int id = 0)
        {
            using (DataContext db = new DataContext())
            {
                Person currentPerson = db.Person.Find(id);
                Address currentAddress = db.Address.Single(a => a.PID == id);

                AutoMapper.Mapper.CreateMap<Person, AdminViewModel>();
                AutoMapper.Mapper.CreateMap<Address, AdminViewModel>();

                MemberViewModel currentMember = AutoMapper.Mapper.Map<AdminViewModel>(currentPerson);

                return View(currentMember);
            }
        }
解决方案

AutoMapper.Map has an overload that takes a source and destination object. So you can take advantage of it using something like this:

Create ViewModels

public class PersonViewModel
{
    //your person fields here..
    //create them with the same name as Person 
    //to avoid having to set each field during mapping  
}

public class AddressViewModel
{
    //your address fields here..
}

public class AdminViewModel
{
    public PersonViewModel Person {get; set;}   

    public AddressViewModel Address {get; set;} 
}

Crete the mappings (You should centralize all your mappings in a class):

public class EntityToViewModelProfile : Profile
{
    protected override void Configure()
    {
        //Create mappings for Person and Address
        AutoMapper.Mapper.CreateMap<Person, AdminViewModel>();
        AutoMapper.Mapper.CreateMap<Address, AdminViewModel>();


        //Create a map to AdminViewModel for both Person and Address

        Mapper.CreateMap<Models.Person, Models.AdminViewModel>()
            .ForMember(dest => dest.Person, opt => opt.MapFrom(src => src));

        Mapper.CreateMap<Models.Address, Models.AdminViewModel>()
            .ForMember(dest => dest.Address, opt => opt.MapFrom(src => src));
    }
}

Register EntityToViewModelProfile in Global.asax.cs

AutoMapper.Mapper.Initialize(x =>
{
    x.AddProfile<EntityToViewModelProfile>();
});

Then, in your controller you use the overload of .Map

using (DataContext db = new DataContext())
{
    Person currentPerson = db.Person.Find(id);
    Address currentAddress = db.Address.Single(a => a.PID == id);

    var mapPerson = AutoMapper.Mapper.Map<Person, AdminViewModel>(currentPerson);

    var mapPersonAndAddress = AutoMapper.Mapper.Map<Address, AdminViewModel>(currentAddress, mapPerson);

    return View(mapPersonAndAddress);
}

这篇关于不能第二实体添加到视图模型与Automapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:44