gpt4 book ai didi

c# - 对象的 map 集合

转载 作者:IT王子 更新时间:2023-10-29 03:59:22 24 4
gpt4 key购买 nike

我第一次尝试将 Automapper 引入应用程序,但我一直收到错误消息,提示我有一些无效参数。

我的模型:

namespace StoreGradesLib.Models
{
public class Store
{
[Key]
public int StoreID { get; set; }

[Required]
[MaxLength(120)]
public string StoreName { get; set; }

[Required]
[MaxLength(20)]
public string StoreNumber { get; set; }

[Required]
[MaxLength(120)]
public string ManagerName { get; set; }

[Required]
public long PhoneNumber { get; set; }

[Required]
public string AddressLine1 { get; set; }

public string AddressLine2 { get; set; }

[Required]
public string Postcode { get; set; }

[Required]
public int WallArea { get; set; }

[Required]
public int FloorArea { get; set; }

[Required]
public int NumWindows { get; set; }

[Required]
public int NumDesks { get; set; }

[Required]
public int NumDoors { get; set; }

[Required]
public int StoreGradeID { get; set; }

[Required]
public bool Active { get; set; }

public virtual StoreGrade StoreGrade { get; set; }

[Timestamp]
public Byte[] Timestamp { get; set; }
}
}

我的 View 模型:

namespace StoreGradesLib.ViewModels
{
public class StoreVM
{
public int StoreID { get; set; }
public bool Active { get; set; }
public Byte[] Timestamp { get; set; }

[Required(ErrorMessage = "Store Name is required.")]
[Display(Name = "Store Name")]
public string StoreName { get; set; }

[Required(ErrorMessage = "Store Number is required")]
public string StoreNumber { get; set; }

[Required(ErrorMessage = "Store Manager is required.")]
[Display(Name = "Manager Name")]
public string ManagerName { get; set; }

[Required(ErrorMessage = "Contact Number is required.")]
[Display(Name = "Phone Number")]
public int PhoneNumber { get; set; }

[Required(ErrorMessage = "Address Line 1 is required.")]
[Display(Name = "Address Line 1")]
public string AddressLine1 { get; set; }

[Display(Name = "Address Line 2")]
public string AddressLine2 { get; set; }

[Required(ErrorMessage = "Postcode is required.")]
public string Postcode { get; set; }

[Required(ErrorMessage = "Must input wall area.")]
[Display(Name = "Wall Area")]
public int WallArea { get; set; }

[Required(ErrorMessage = "Must input floor area.")]
[Display(Name = "Floor Area")]
public int FloorArea { get; set; }

[Required(ErrorMessage = "Must input number of windows.")]
[Display(Name = "Windows")]
public int NumWindows { get; set; }

[Required(ErrorMessage = "Must input number of desks.")]
[Display(Name = "Desks")]
public int NumDesks { get; set; }

[Required(ErrorMessage = "Must input number of doors.")]
[Display(Name = "Doors")]
public int NumDoors { get; set; }

[Required(ErrorMessage = "Store must have a grade.")]
public StoreGrade StoreGradeID { get; set; }

public string Address
{
get
{
return StoreName + " " + AddressLine1 + " " + AddressLine2 + " " + Postcode;
}
}
}
}

创建的映射:

CreateMap<Store, StoreVM>();
CreateMap<StoreVM, Store>();

在我的 Controller 中,我试图将一系列商店映射到 storeVM。我目前正在这样购买我的商店;

var stores = db.Store.Include(s => s.StoreGrade);
stores = from s in db.Store.Where(s => s.Active.Equals(true))
select s;

我想将选择的商店映射到 StoreVM,我尝试了以下操作,但收到无效参数警告,

var VMstores = Mapper.Map<Store, StoreVM>(stores);

我收到最佳重载方法匹配有一些无效参数的错误。

谁能指出我正确的方向?

最佳答案

您正在映射集合,而不是单个实体( IEnumerable<Store>IEnumerable<StoreVM> ),所以使用此映射

var VMstores = Mapper.Map<IEnumerable<Store>, IEnumerable<StoreVM>>(stores);

关于c# - 对象的 map 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13687240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com