0
我試圖將模型映射到實體對象,但是當我映射從DB接收的實體對象時 - 我的更改不保存在數據庫中。使用Automapper僅映射特定屬性
下面是一些代碼:
映像配置:
Mapper.CreateMap<WalletUpdateModel, Wallet>()
.ForMember(o => o.Name, m => m.MapFrom(d => d.Name))
.ForMember(o => o.Currency, m => m.MapFrom(d => d.Currency));
實體對象:
public partial class Wallet
{
public Wallet()
{
this.Transactions = new HashSet<Transaction>();
}
public int Id { get; set; }
public int Owner { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
}
模型聲明:
public class WalletUpdateModel
{
[Required]
[Range(1, int.MaxValue)]
public int Id { get; set; }
[Required]
[MaxLength(128)]
public string Name { get; set; }
[Required]
[MaxLength(3)]
[DataType(DataType.Currency)]
public string Currency { get; set; }
}
而從即時窗口中一些調試信息:
w (before mapping)
{System.Data.Entity.DynamicProxies.Wallet_E3CA830BB5384920A3E07D4B44F15D2409093A34BFCED7CA33F9EC4102445554}
[System.Data.Entity.DynamicProxies.Wallet_E3CA830BB5384920A3E07D4B44F15D2409093A34BFCED7CA33F9EC4102445554]: {System.Data.Entity.DynamicProxies.Wallet_E3CA830BB5384920A3E07D4B44F15D2409093A34BFCED7CA33F9EC4102445554}
Currency: "PLN"
Id: 2
Name: "Cash"
Owner: 1
Transactions: Count = 0
w (after mapping)
{Financica.WebServices.Wallet}
Currency: "PLN"
Id: 2
Name: "BZWBK"
Owner: 0
Transactions: Count = 0
請幫我解決這個問題,謝謝。
您能否確切地確認問題是您需要幫助的問題?例如,映射有什麼問題嗎?如果是這樣,什麼是錯誤映射? – Mightymuke
我試圖更新從數據庫使用模型映射從數據庫接收的數據實體,但正如您在最後一次代碼偵聽(從即時窗口)中看到的,automapper不會正確映射值。 – vchyzhevskyi