最近,需要搭建一個新項目,在需求分析時確定數據庫中需要創建多個存儲過程。所以如果還是用原來EF框架生成ADO.net實體模型的話,不利於修改。
主要是解決以下兩個問題:
1、比如前端需要一個值,如果存儲過程沒有返回,那么在修改存儲過程后就得更新實體。很麻煩。
2、前端所需數據類型和返回數據類型不同時直接能映射不需要循環處理。
下面做一個簡單的用法介紹(以機場數據為例):
第一個問題:
1、首先用petapoco鏈接數據庫
下載鏈接:http://pan.baidu.com/s/1dFEfzSd
將文件下載后放在一個文件夾中,然后再這個文件夾中創建一個類文件用於創建與數據鏈接的橋梁。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Web; 7 using PetaPoco; 8 9 namespace Entity.Models 10 { 11 public class LxaAirportStatistics 12 { 13 public static Database Db 14 { 15 get 16 { 17 if (HttpContext.Current.Items["CurrentDb"] == null) 18 { 19 var retval = new Database("AirportEntity"); 20 HttpContext.Current.Items["CurrentDb"] = retval; 21 return retval; 22 } 23 return (Database)HttpContext.Current.Items["CurrentDb"]; 24 } 25 } 26 27 } 28 }
文件中Database的name與web.config中數據庫中鏈接字符串的name一樣。
2、現在數據庫鏈接創建好了,就需要創建一個model類用於對數據建立關系。model類字段名稱、類型必須和數據庫字段名稱、類型一致。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Entity 7 { 8 public class Airport 9 { 10 public string Airport_DM { get; set; } 11 public int? Area_DM { get; set; } 12 public string JC { get; set; } 13 public string QC { get; set; } 14 public string TestCol { get; set; } 15 } 16 }
至此,數據庫鏈接和對應關系都創建好了。第一個問題已經解決?有人會問怎么就解決了,那么我可以告訴你,現在數據庫的存儲過程修改后添加了字段或者刪除了字段,只需要在對應的model類對應修改就行,不在需要想使用EF框架一樣還得重新生成實體。不信你試試。接下來我們解決第二個問題。
第二個問題:
現在數據雖然從數據庫拿出來了,但是前端只需要一部分字段,且類型不同怎么辦?
1、創建一個和前端需要的數據類型一樣的model類,當然名字的和數據庫對應的model類型區別開。

1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Threading.Tasks; 5 6 namespace IBLL.Dto 7 { 8 public class AirportDto 9 { 10 public string Airport_DM { get; set; } 11 public int? Area_DM { get; set; } 12 public string JC { get; set; } 13 public string QC { get; set; } 14 } 15 }
2、數據模型映射到前端需要的模型,這就需要AutoMapper
下載地址:http://pan.baidu.com/s/1c2Knjfa
引用了dll文件之后就需要配置Automapper

1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Threading.Tasks; 5 using AutoMapper; 6 using IBLL.Dto; 7 8 9 namespace BLL.AutoMapper 10 { 11 public class AutoMapperConfig 12 { 13 public static void Configure() 14 { 15 Mapper.Initialize(x => 16 { 17 x.CreateMap<double?, string>().ConvertUsing<DoubleToString>();//將數據模型中double?類型全部轉化成string 18 x.CreateMap<Entity.Airport, AirportDto>(); 19 20 }); 21 } 22 23 private class DoubleToString : ITypeConverter<double?, string> 24 { 25 public string Convert(double? source, string destination, ResolutionContext context) 26 { 27 return source == null ? "" : source?.ToString("0.00").TrimEnd('0').TrimEnd('.'); 28 } 29 } 30 } 31 }
我這還封裝了一下,也一並分享給大家

1 using AutoMapper; 2 using System; 3 using System.Collections; 4 using System.Collections.Generic; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace BLL.AutoMapper 9 { 10 /// <summary> 11 /// AutoMapper擴展幫助類 12 /// </summary> 13 public static class AutoMapperHelper 14 { 15 /// <summary> 16 /// 類型映射 17 /// </summary> 18 public static T MapTo<T>(this object obj) 19 { 20 if (obj == null) return default(T); 21 22 return Mapper.Map<T>(obj); 23 } 24 /// <summary> 25 /// 集合列表類型映射 26 /// </summary> 27 public static List<TDestination> MapToList<TDestination>(this IEnumerable source) 28 { 29 30 return Mapper.Map<List<TDestination>>(source); 31 } 32 33 34 } 35 }
3、規則配置完成,現在就剩最后一步。實現對象的完美轉換。

1 using IBLL; 2 using System; 3 using System.Collections.Generic; 4 using System.Text; 5 using System.Threading.Tasks; 6 using AutoMapper; 7 using BLL.AutoMapper; 8 using IBLL.Dto; 9 using Entity.Models; 10 11 namespace BLL 12 { 13 public class BLLAirport : IAirport 14 { 15 //獲取所有機場 16 public List<AirportDto> GetALLAirpotsList() 17 { 18 var listAirport = AirportEntity.Db.Fetch<Entity.Airport>("select * from Airports); 19 return listAirport.MapToList<AirportDto>(); 20 21 } 22 } 23 }
至此數據結果已經轉換。兩個問題也解決。當然項目結構不是完整的,大家應該看得懂的。具體的文件位置可根據自己的項目結構進行調整。
這僅僅是我在項目中遇到時解決的辦法,有什么問題或者有其他的辦法請大家多多指教。謝謝!