2013-06-09 36 views
5

Tôi đã thấy rất nhiều ví dụ về lỗi này xảy ra, vì nhiều nguyên nhân và tôi đã trải qua tất cả các nguyên nhân tôi có thể thấy, nhưng tôi vẫn nhận được các lỗi, vì vậy tôi tự hỏi nếu một số người có thể cung cấp cho một số thông tin về những gì lỗi này thực sự có nghĩa là, vì vậy tôi có thể thử tìm nguyên nhân. Dưới đây là một số mã:Làm thế nào để gỡ lỗi AutoMapper "Thiếu kiểu bản đồ cấu hình hoặc bản đồ không được hỗ trợ" lỗi

Bộ điều khiển:

[HttpPost] 
    public ActionResult Edit(ProfileViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var person = new UserAttribute(); 

      person = Mapper.Map<ProfileViewModel, UserAttribute>(model); 

      db.UserAttribute.Add(person); 
      db.SaveChanges(); 

     } 

Xem Mẫu

public class ProfileViewModel 
{ 

    [Display(Name = "First Name")] 
    [StringLength(20)] 
    [Required] 
    public string FirstName { get; set; } 

    [Display(Name = "Last Name")] 
    [StringLength(30)] 
    [Required] 
    public string LastName { get; set; } 

    [Display(Name = "Gender")] 
    [Required] 
    public string Gender { get; set; } 

    [Display(Name = "Date of Birth")] 
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
    public DateTime DOB { get; set; } 

    [Display(Name = "Hair Color")] 
    public string HairColor { get; set; } 

    [Display(Name = "Eye Color")] 
    public string EyeColor { get; set; } 

    [Display(Name = "Body Type")] 
    public string Weight { get; set; } 

    [Display(Name = "Height")] 
    public string HeightFeet { get; set; } 

    public string HeightInches { get; set; } 

    public int UserId { get; set; } 

    public IEnumerable<SelectListItem> WeightList { get; set; } 
    public IEnumerable<SelectListItem> EyeColorList { get; set; } 
    public IEnumerable<SelectListItem> HairColorList { get; set; } 
    public IEnumerable<SelectListItem> HeightFeetList { get; set; } 
    public IEnumerable<SelectListItem> HeightInchesList { get; set; } 
    public IEnumerable<SelectListItem> GenderList { get; set; } 

} 

mô hình thuộc tính người dùng:

public int ProfileId { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Gender { get; set; } 
    public System.DateTime DOB { get; set; } 
    public string HairColor { get; set; } 
    public string EyeColor { get; set; } 
    public string HeightFeet { get; set; } 
    public string Weight { get; set; } 
    public int UserId { get; set; } 
    public string HeightInches { get; set; } 

Mapping config:

public class AutoMapperConfiguration 
{ 
    public static void Configure() 
    { 
     Mapper.Initialize(x => x.AddProfile<ViewToDomainMapProfile>()); 
     Mapper.Initialize(x => x.AddProfile<DomainToViewMapProfile>()); 
    } 
} 

public class ViewToDomainMapProfile : Profile 
{ 
    public override string ProfileName 
    { 
     get { return "ViewToDomainMapProfile"; } 
    } 

    protected override void Configure() 
    { 
     Mapper.CreateMap<ProfileViewModel, UserAttribute>() 
      .ForSourceMember(x => x.GenderList, y => y.Ignore()) 
      .ForSourceMember(x => x.HairColorList, y => y.Ignore()) 
      .ForSourceMember(x => x.EyeColorList, y => y.Ignore()) 
      .ForSourceMember(x => x.WeightList, y => y.Ignore()) 
      .ForSourceMember(x => x.HeightFeetList, y => y.Ignore()) 
      .ForSourceMember(x => x.HeightInchesList, y => y.Ignore()); 
    } 
} 

và cấu hình được gọi là trong tuyến Asax toàn cầu:

 AutoMapperConfiguration.Configure(); 
+0

đây có phải là lỗi biên dịch hay thời gian chạy không? Ngoài ra, không để miền ánh xạ viewmodel hoạt động đúng cách? – user1778606

Trả lời

0

Đối viewmodel => thuộc tính người dùng

tôi nhận thấy rằng ProfileId là một tài sản đích, nhưng không phải là một tài sản nguồn.

public int ProfileId { get; set; } 

Bạn có cần thêm mã để nhập thành viên đích này không?

Khác: Tôi cũng có thể đề xuất sử dụng hoặc tùy chỉnh trình tự động hóa để ánh xạ các thuộc tính hiển thị đối sánh đó theo tên độc quyền.

Ngoài ra, khi có thể, vui lòng tránh các tên mẫu kết thúc bằng từ Attritribute theo quy ước này được sử dụng gần như dành riêng cho các thuộc tính thực tế. (Lời xin lỗi của tôi cho soi mói)

10

Sử dụng Mapper.AssertConfigurationIsValid(); tạo ra ngoại lệ sau đây:

AutoMapper.AutoMapperConfigurationException : 
Unmapped members were found. Review the types and members below. 
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type 
============================================================================================== 
ProfileViewModel -> UserAttribute (Destination member list) 
---------------------------------------------------------------------------------------------- 
ProfileId 

Vì vậy, bạn cần phải thêm bản đồ cho ProfileId.

Nhìn chung, thực hành tốt là sử dụng Mapper.AssertConfigurationIsValid(); hoặc trong các thử nghiệm đơn vị của bạn (bạn có chúng, phải không?) Hoặc sau cấu hình người lập bản đồ của bạn. Nó sẽ hiển thị thông tin chi tiết cho một cấu hình sai như vậy.

+0

Điều này đã lưu tôi. Chỉ cần thêm Mapper.AssertConfigurationIsValid(); trong ánh xạ global.asax của tôi cho tôi thấy rằng ánh xạ của tôi đang sử dụng một lớp khác có cùng tên. (bỏ qua rằng đó không phải là một thực hành tuyệt vời, đó là mã di sản) Tôi đã không nhận ra các Mapper.Map() sẽ không ném một lỗi nếu nó không thể tìm thấy ánh xạ cho tất cả các thành viên. Nó chỉ thất bại khi nó cố gắng để bản đồ. –

+0

ai đó nên chấp nhận câu trả lời này –