本文介绍了AutoMapper更新UseDestinationValue在虚拟属性上未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在试图更新现有供应商和关联的供应商联系人的情况下,努力使AutoMapper(6.1.1)能够正常工作.
Struggling to get AutoMapper (6.1.1) to work in this scenario of attempting to update an existing Vendor and associated Vendor Contacts.
我尝试在相关实体上使用 .ignore()
和 .UseDestinationValues()
,但无济于事.
I've tried using .ignore()
and .UseDestinationValues()
on the related entities, both to no avail.
以下是映射后目标值发生的情况:
Here's what happens to the destination values after the map:
- existingStratusVendor.Id = 0(应为现有值)
- existingStratusVendor.VendorContacts.Id = 0(应为现有值)
- existingStratusVendor.Items = null,但在映射之前具有1个相关实体,与所有其他相关虚拟属性相同.(对于我标记为
.UseDestinationValues()
的所有其他虚拟属性,也会发生这种情况)
- existingStratusVendor.Id = 0 (should be value of existing)
- existingStratusVendor.VendorContacts.Id = 0 (should be value of existing)
- existingStratusVendor.Items = null, but had 1 related entity prior to mapping, same with all other related virtual properties. (this happens for all other virtual properties that I've marked as
.UseDestinationValues()
as well)
我在做什么错或者我误解了它应该如何工作?
What am I doing wrong or am I misunderstanding how this is supposed to work?
POCO
public partial class Vendor
{
public Vendor()
{
this.Items = new HashSet<Item>();
this.Items1 = new HashSet<Item>();
this.VendorContacts = new HashSet<VendorContact>();
this.POHeaders = new HashSet<POHeader>();
this.ReceiptHeaders = new HashSet<ReceiptHeader>();
this.ItemPriceCostRules = new HashSet<ItemPriceCostRule>();
}
public int Id { get; set; }
public int CompanyId { get; set; }
public string VendorName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Notes { get; set; }
public int CreatedById { get; set; }
public System.DateTime CreatedOn { get; set; }
public int ModifiedById { get; set; }
public System.DateTime ModifiedOn { get; set; }
public string FinancialsId { get; set; }
public int LeadTimeDays { get; set; }
public int SafetyStockDays { get; set; }
public virtual ICollection<Item> Items { get; set; }
public virtual ICollection<Item> Items1 { get; set; }
public virtual ICollection<VendorContact> VendorContacts { get; set; }
public virtual ICollection<POHeader> POHeaders { get; set; }
public virtual Company Company { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual UserProfile UserProfile1 { get; set; }
public virtual ICollection<ReceiptHeader> ReceiptHeaders { get; set; }
public virtual ICollection<ItemPriceCostRule> ItemPriceCostRules { get; set; }
}
public partial class VendorContact
{
public int Id { get; set; }
public int VendorId { get; set; }
public string ContactName { get; set; }
public string EmailAddress { get; set; }
public string OfficePhone { get; set; }
public string CellPhone { get; set; }
public int CreatedById { get; set; }
public System.DateTime CreatedOn { get; set; }
public int ModifiedById { get; set; }
public System.DateTime ModifiedOn { get; set; }
public bool PurchasingContact { get; set; }
public virtual Vendor Vendor { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual UserProfile UserProfile1 { get; set; }
}
地图
CreateMap<Vendor, Vendor>()
.ForMember(dest => dest.Id, option => option.UseDestinationValue())
.ForMember(dest => dest.Company, option => option.UseDestinationValue())
.ForMember(dest => dest.POHeaders, option => option.UseDestinationValue())
.ForMember(dest => dest.ReceiptHeaders, option => option.UseDestinationValue())
.ForMember(dest => dest.Items, option => option.UseDestinationValue())
.ForMember(dest => dest.Items1, option => option.UseDestinationValue())
.ForMember(dest => dest.ItemPriceCostRules, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile1, option => option.UseDestinationValue())
;
CreateMap<VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.UseDestinationValue())
.ForMember(dest => dest.VendorId, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile1, option => option.UseDestinationValue())
代码
public ActionConfirmation<int> ImportFromFinancials(Vendor financialsModifiedVendor, int intUserId)
{
Vendor vendorToUpdate;
var existingStratusVendor = _vendorRepository
.SearchFor(a => a.CompanyId == intCompanyId && a.FinancialsId == financialsModifiedVendor.FinancialsId).FirstOrDefault();
if (existingStratusVendor == null) //add a new vendor
{
vendorToUpdate = financialsModifiedVendor;
}
else
{
Mapper.Map(financialsModifiedVendor, existingStratusVendor);
vendorToUpdate = existingStratusVendor;
}
//Save Vendor
var baseAppServ = new BaseAppServ<Vendor>(_repository);
var vendorUpdateResult = baseAppServ.SaveOrUpdate(vendorToUpdate, intUserId);
if (!vendorUpdateResult.WasSuccessful) return vendorUpdateResult;
...
}
推荐答案
两个实体都具有相同的名称,似乎您缺少名称空间
both entities has the same name, it looks like you are missing a namespace
CreateMap<Other.Namespace.VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.UseDestinationValue())
.ForMember(dest => dest.VendorId, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile, option => option.UseDestinationValue())
.ForMember(dest => dest.UserProfile1, option => option.UseDestinationValue())
这篇关于AutoMapper更新UseDestinationValue在虚拟属性上未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!