AutoMapper主要用來處理類別間的轉換
官方連結
簡單範例
- 建立一個Model
1 | |
- 建立對應的ViewModel,只需要顯示MemberID和NickName
1 | |
- 一般類別轉換
1 | |
- AutoMapper寫法
1 | |
- Linq轉換IEnumerable
1 | |
- AutoMapper轉換IEnumerable
1 | |
AutoMapper全域設定 : 如果轉換有多個地方使用,可以寫在全域
- 建立Model、ViewModel
1 | |
- 在App_Start新增AutoMapperConfig
1 | |
- 在App_Start新增MemberInfoProfile
1 | |
- 在Global.asax呼叫
1 | |
- 使用方式
1 | |
轉換設定方式
- Ignore: 略過
- MapFrom: 指定對應屬性
- ResolveUsing: 指定自訂的轉換方式
- UseValue: 直接給值
- NullSubstitute: 原始值為空才給值
- Condition: 條件為True才給值