在尝试更新现有供应商和关联的供应商联系人的情况下,努力使AutoMapper(6.1.1)能够正常工作。
我尝试在相关实体上使用.ignore()
和.UseDestinationValues()
,但都无济于事。
以下是映射后目标值发生的情况:
.UseDestinationValues()
的所有其他虚拟属性中也会发生这种情况)我在做什么错或者我误解了它应该如何工作?
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;
...
}
答案 0 :(得分:0)
两个实体都具有相同的名称,好像您缺少名称空间
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())