2. demo中使用到的類
public class order2. 物件對映情況public decimal? totalamount
public bool? isenable
public orderdetail orderdetaientity
public listproductlist
}public class orderdetail
public datetime? orderdate
public string orderuser
public datetime? closeddate
}public class productdetail
public string productname
public decimal? price
}public class orderdto
public decimal totalamount
public bool isenable
public string remark
public orderdetaildto orderdetaientity
public listproductlist
}public class orderdetaildto
public string orderuser
public datetime? closeddate
}public class productdetaildto
public string productname
public decimal? price
}
(1) 屬性型別和名稱相同的物件之間直接對映
productdetail product = new productdetail();
(2) 物件之間屬性轉換、屬性忽略
.formember(dest => dest.id, opt => opt.mapfrom(src => src.id))
.formember(dest => dest.totalamount, opt => opt.mapfrom(src => src.totalamount ?? 0))
.formember(dest => dest.isenable, opt => opt.mapfrom(src => src.isenable ?? false))
.formember(dest => dest.remark, opt => opt.mapfrom(src => src.orderdetaientity.remark))
.formember(dest => dest.orderdetaientity, opt => opt.ignore())
(3)屬性填充固定值
.formember(dest => dest.orderdate, opt => opt.usevalue(datetime.now));
(4)包含子物件的物件對映,需要先對映子物件
.formember(dest => dest.orderdate, opt => opt.usevalue(datetime.now));
.formember(dest => dest.id, opt => opt.mapfrom(src => src.id))
.formember(dest => dest.totalamount, opt => opt.mapfrom(src => src.totalamount ?? 0))
.formember(dest => dest.isenable, opt => opt.mapfrom(src => src.isenable ?? false))
.formember(dest => dest.remark, opt => opt.mapfrom(src => src.orderdetaientity.remark));
或者可以這樣寫
.formember(dest => dest.id, opt => opt.mapfrom(src => src.id))
.formember(dest => dest.totalamount, opt => opt.mapfrom(src => src.totalamount ?? 0))
.formember(dest => dest.isenable, opt => opt.mapfrom(src => src.isenable ?? false))
.formember(dest => dest.remark, opt => opt.mapfrom(src => src.orderdetaientity.remark));
//對映子物件
exp.formember(dest => dest.orderdetaientity, opt => opt.mapfrom(dto => new orderdetaildto() ));
或者也可以這樣寫
.formember(dest => dest.orderdate, opt => opt.usevalue(datetime.now));
();()
.formember(dest => dest.id, opt => opt.mapfrom(src =>src.id))
.formember(dest => dest.totalamount, opt => opt.mapfrom(src => src.totalamount ?? 0
)) .formember(dest => dest.remark, opt =>opt.ignore())
.formember(dest => dest.isenable, opt =>opt.ignore())
//包含多個相同子物件時
(src.orderdetaientity)
));(5)轉化存在的物件
.formember(dest => dest.orderdate, opt => opt.usevalue(datetime.now));
.formember(dest => dest.id, opt => opt.mapfrom(src => src.id))
.formember(dest => dest.totalamount, opt => opt.mapfrom(src => src.totalamount ?? 0))
.formember(dest => dest.remark, opt => opt.ignore())
.formember(dest => dest.isenable, opt => opt.ignore());
var orderdto4 = new orderdto() ;
(6)其他的, 通過自定義轉化來實現
如可以實現 typeconverter, valueresolver,valueformatter 抽象方法.
3.其他說明
(1)檢查還有那些規則沒有寫完整,可以在最後呼叫下面的方法
(2)重新配置對映
4. 參考**
AutoMapper 使用實踐
一.使用意圖 從我開發過程使用到一些場景 實體 實體 集合 集合 實體欄位名稱不同 實體資料型別不同 相同名稱,相同資料型別無需配置 三.最佳實踐 專案結構 每個專案用途,解決方案資料夾基本標示清楚。2.以訂單為例 不是真實業務,只是舉個簡單的例子 在models 實體類庫 新增ordermodel...
AutoMapper使用筆記
主要用途 領域物件與dto之間的轉換 資料庫查詢結果對映至實體物件。使用筆記 場景1 源型別blogentry,目標型別blogpostdto,指定屬性進行對映 blogentry.id對應於blogpostdto.postid formember dto dto.postid,opt opt.ma...
AutoMapper使用筆記
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!主要用途 領域物件與dto之間的轉換 資料庫查詢結果對映至實體物件。使用筆記 場景1 源型別blogentry,目標型別blogpostdto,指定屬性進行對映 blogentry.id對應於blogpostdto.postid 場景2 idata...