0

努力让 AutoMapper (6.1.1) 在尝试更新现有供应商和相关供应商联系人的情况下工作。

我已经尝试在相关实体上使用.ignore().UseDestinationValues(),但均无济于事。

以下是映射后目标值发生的情况:

  1. existingStratusVendor.Id = 0(应该是现有的值)
  2. existingStratusVendor.VendorContacts.Id = 0(应该是现有的值)
  3. existingStratusVendor.Items = null,但在映射之前有 1 个相关实体,与所有其他相关虚拟属性相同。(这也发生在我标记的所有其他虚拟属性上.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;
...
}
4

1 回答 1

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())
于 2018-06-27T19:43:24.443 回答