首页 > 解决方案 > 将多个不同的模型映射到单个视图模型中 - AutoMapper

问题描述

我正在尝试将多个 Userprofile 模型映射到单个 viewModel 中。一次只会映射一个用户数据。

在下面的代码片段中,我提到了我已经尝试过的用例。此外,我尝试AutoMapper.Map<source,target>但没有为我工作。

//这是一个 FarmerUser 模型

 public partial class FarmerProfileModel
    {      
    public long FarmerId { get; set; }

    public string FarmerName { get; set; }

    public string FatherHusbandName { get; set; }

    public string Cnic { get; set; }     

    public string Gender { get; set; }

    public string CellPhone { get; set; }

    public string PresentAddress { get; set; }

    public string PermanentAddress { get; set; }

    public int? EducationCode { get; set; }

    public int? MaleDependant { get; set; }

    public int? FemaleDependant { get; set; }

    public string AlternateName { get; set; }

    public string AlternateCellPhoneNo { get; set; }

    public string AlternateRelationshipwithFarmer { get; set; }

    public int? ReferalCode { get; set; }

    public string FarmerImage { get; set; }

    public string UnionCouncil { get; set; }

    public string MozaName { get; set; }

    public int? SmallAnimals { get; set; }

    public int? BigAnimals { get; set; }

    public int? Tractor { get; set; }

    public Guid? UserGuid { get; set; }

    public DistrictCodeModel DistrictCodeNavigation { get; set; }

    public TehsilCodeModel TehsilCodeNavigation { get; set; }     
   }

Tso 用户的另一种模式

public class TsoProfileModel
  {
    public long Tsoid { get; set; }

    public string Tsoname { get; set; }

    public string Cnic { get; set; }

    public string Email { get; set; }

    public string CellPhone { get; set; }

    public string AlternateCellPhone { get; set; }

    public string Landline { get; set; }

    public string Gender { get; set; }

    public string Tsoimage { get; set; }

    public int? ModifiedBy { get; set; }

    public DateTime? ModifiedDateTime { get; set; }

    public DateTime? InsertionDate { get; set; }

    public string ActiveStatus { get; set; }

    public Guid? UserGuid { get; set; }

    public TbDistrictCode DistrictCodeNavigation { get; set; }

    public TbTehsilCode TehsilCodeNavigation { get; set; }

   }

这是我的 ViewModel,我试图在其中合并我的 Farmer/Tso 数据

   public class UserProfileInfo
   {
    public long? UserId { get; set; }
    public string UserName { get; set; }
    public string Cnic { get; set; }
    public string Email { get; set; }
    public string AlternateCellPhone { get; set; }
    public string Landline { get; set; }
    public string Gender { get; set; }
    public string PresentAddress { get; set; }
    public string PermanentAddress { get; set; }
    public int? EducationCode { get; set; }
    public string image { get; set; }
    public int? ModifiedBy { get; set; }
    public DateTime? ModifiedDateTime { get; set; }
    public DateTime? InsertionDate { get; set; }
    public string ActiveStatus { get; set; }
    public Guid? UserGuid { get; set; }
    public string FatherHusbandName { get; set; }
    public string CellPhone { get; set; }
    public int? MaleDependant { get; set; }
    public int? FemaleDependant { get; set; }
    public string AlternateName { get; set; }
    public string AlternateRelationshipwithFarmer { get; set; }
    public int? ReferalCode { get; set; }
    public string UnionCouncil { get; set; }
    public string MozaName { get; set; }
    public int? SmallAnimals { get; set; }
    public int? BigAnimals { get; set; }
    public int? Tractor { get; set; }
    public string Role { get; set; }
    public DistrictCodeModel DistrictCodeNavigation { get; set; }
    public TehsilCodeModel TehsilCodeNavigation { get; set; }    
    }

下面是我尝试使用的代码。

if (User=="farmer")
        {


            var tbFarmerInfo = await 
   _farmerService.GetFarmerByCellPhone(cellno);
            var result = _Mapper.Map<UserProfileInfo>(tbFarmerInfo);

            result.UserId = tbFarmerInfo.FarmerId;
            result.UserName = tbFarmerInfo.FarmerName;
            result.image = tbFarmerInfo.FarmerImage;
            result.Role = "Farmer";
            response.Data = result;

            return response;
        }
  else if (User == "TSO")
   {
            string cellno = User.Claims.FirstOrDefault(c => c.Type == 
    ClaimTypes.Name).Value.TrimStart('0');


            var tbTsoInfo = await _tsoService.GetTSOByCellPhone(cellno);
            var result = _Mapper.Map<UserProfileInfo>(tbTsoInfo);

            result.UserId = tbTsoInfo.Tsoid;
            result.UserName = tbTsoInfo.Tsoname;
            result.image = tbTsoInfo.Tsoimage;
            result.Role = "TSO";
            response.Data = result;

           return response;
}

预期的结果应该是两个模型都可以映射到 viewModel 中。就像,如果我映射 FarmerProfile 它应该被映射,如果我想映射 TsoProfile 它也应该被映射。

标签: c#.net-coreautomapper

解决方案


推荐阅读