升级版本:
NetCore sdk 2.2.108 、AspNetCore 2.2.0、EFCore 2.2.6
所有程序引用均从NuGet上下载,并支持NetCore
问题:
问题1:No coercion operator is defined between types 'System.Int16' and 'System.Boolean'(mysql 数据库类型转化中int16 byte bool 转化问题)
解决方案:
引用 Pomelo.EntityFreamworkCore.Mysql 2.2.0 代替 MySql.Data.EntityFrameworkCore
问题2:Error generated for warning 'Microsoft.EntityFrameworkCore.Database.Transaction.AmbientTransactionWarning: An ambient transaction has been detected. The current provider does not support ambient transactions.(环境事务、分布式事务问题)
解决方案:
因为Pomelo.EFCore.Mysql 2.2.0 不支持TransactionScope
所以 采用 using (var scope = DataContextFactory.GetDataContext().Database.BeginTransaction(System.Data.IsolationLevel.RepeatableRead)) 或者
using (var scope = new CommittableTransaction()) 这两种事务代替
问题3:HttpResponseMessage 问题
解决方案:
两个解决方案,建议第一个
1:改为File()返回 2:添加引用 并在startup里注册 services.AddMvc().AddWebApiConventions();
问题4:依赖问题
解决方案:采用Autofac4.9.3 版本支持NetCore
问题5:模型转换 Model 不支持static
解决方案:将之前的代码改为第二个里面的 ,并在调用的地方统一 实例化
public center_flow CenterFlowDto_to_CenterFlow(CenterFlowDto dto) { var mapper = CreateMap<CenterFlowDto, center_flow>(); return mapper.Map<CenterFlowDto, center_flow>(dto); }
/// <summary> /// 险别列表 /// </summary> /// <param name="list"></param> /// <returns></returns> public List<compensate_insurance> CompensateInsuranceDtoList_to_EntityList(List<CompensateInsuranceDto> list) { //var mapper = CreateMap<CompensateAdjustmentDto, compensate_adjustment>(); //return mapper.Map<List<CompensateInsuranceDto>, List<compensate_insurance>>(list); var config = new MapperConfiguration(cfg => { cfg.CreateMap<List<CompensateInsuranceDto>, List<compensate_insurance>>(); }); var mapper = config.CreateMapper(); return mapper.Map<List<CompensateInsuranceDto>, List<compensate_insurance>>(list); }
帮助类:SqlQuery<T>
1 public static class DataContextExtend 2 { 3 public static List<T> SqlQuery<T>(this DatabaseFacade facade, string sql, params object[] parameters) 4 { 5 using (var conn = new MySqlConnection(facade.GetDbConnection().ConnectionString)) 6 { 7 conn.Open(); 8 var ds = new DataSet(); 9 using (var da = new MySqlDataAdapter(sql, conn.ConnectionString)) 10 { 11 if (parameters.Any()) 12 { 13 da.SelectCommand.Parameters.AddRange(parameters); 14 } 15 da.Fill(ds); 16 } 17 18 conn.Close(); 19 if (ds.Tables.Count > 0) 20 { 21 return ToDataList<T>(ds.Tables[0]); 22 } 23 else 24 { 25 return new List<T>(); 26 } 27 } 28 } 29 /// <summary> 30 /// 返回首行首列 31 /// </summary> 32 /// <typeparam name="T"></typeparam> 33 /// <param name="facade"></param> 34 /// <param name="sql"></param> 35 /// <param name="parameters"></param> 36 /// <returns></returns> 37 public static T SqlCount<T>(this DatabaseFacade facade, string sql, params object[] parameters) 38 { 39 using (var conn = new MySqlConnection(facade.GetDbConnection().ConnectionString)) 40 { 41 conn.Open(); 42 T result; 43 var ds = new DataSet(); 44 45 using (var commend = new MySqlCommand(sql, conn)) 46 { 47 if (parameters.Any()) 48 commend.Parameters.AddRange(parameters); 49 result = (T)commend.ExecuteScalar(); 50 } 51 52 conn.Close(); 53 54 return result; 55 56 } 57 58 } 59 60 61 /// <summary> 62 /// DataTable转成List 63 /// </summary> 64 /// <typeparam name="T"></typeparam> 65 /// <param name="dt"></param> 66 /// <returns></returns> 67 public static List<T> ToDataList<T>(this DataTable dt) 68 { 69 var list = new List<T>(); 70 var plist = new List<PropertyInfo>(typeof(T).GetProperties()); 71 if (typeof(T) == typeof(string)) 72 { 73 foreach (DataRow givenObject in dt.Rows) 74 { 75 for (int i = 0; i < dt.Columns.Count; i++) 76 { 77 if (givenObject[i] != DBNull.Value && givenObject[i] != null) 78 { 79 var instance = givenObject[i].ToString(); // Your custom conversion to string. 80 list.Add((T)(object)instance); 81 } 82 else 83 { 84 list.Add((T)(object)null); 85 } 86 } 87 } 88 } 89 else if (typeof(T).IsClass) 90 { 91 foreach (DataRow item in dt.Rows) 92 { 93 T s = Activator.CreateInstance<T>(); 94 for (int i = 0; i < dt.Columns.Count; i++) 95 { 96 PropertyInfo info = plist.Find(p => p.Name.ToUpper() == dt.Columns[i].ColumnName.ToUpper()); 97 if (info != null) 98 { 99 try 100 { 101 if (!Convert.IsDBNull(item[i])) 102 { 103 object v = null; 104 if (info.PropertyType.ToString().Contains("System.Nullable")) 105 { 106 v = Convert.ChangeType(item[i], Nullable.GetUnderlyingType(info.PropertyType)); 107 } 108 else 109 { 110 if (info.PropertyType.BaseType != null && info.PropertyType.BaseType.ToString().Contains("System.Enum")) 111 { 112 //将字符串转换为枚举对象 113 //v = Enum.Parse(info.PropertyType, item[i].ToString()); 114 v = int.Parse(item[i].ToString()); 115 } 116 else 117 { 118 v = Convert.ChangeType(item[i], info.PropertyType); 119 } 120 } 121 info.SetValue(s, v, null); 122 } 123 } 124 catch (Exception ex) 125 { 126 throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message); 127 } 128 } 129 } 130 list.Add(s); 131 } 132 } 133 else if (typeof(T).BaseType == typeof(System.ValueType)) 134 { 135 try 136 { 137 foreach (DataRow item in dt.Rows) 138 { 139 for (int i = 0; i < dt.Columns.Count; i++) 140 { 141 if (item[i] != DBNull.Value && item[i] != null) 142 { 143 if (plist.Count > 0) 144 { 145 list.Add((T)((object)Convert.ChangeType(item[i], plist.LastOrDefault()?.PropertyType))); 146 } 147 else 148 { 149 if (item[i].GetType().ToString() == "System.Int64" && typeof(T).FullName == "System.Int32") 150 { 151 list.Add((T)((object)int.Parse(item[i].ToString()))); 152 } 153 else 154 { 155 list.Add((T)((object)item[i])); 156 } 157 } 158 } 159 else 160 { 161 list.Add((T)(object)null); 162 } 163 } 164 } 165 } 166 catch (Exception ex) 167 { 168 throw new Exception("类型[" + typeof(T) + "]转换出错," + ex.Message); 169 } 170 } 171 return list; 172 } 173 174 }
Nuget 引用及版本
帮助类:
Model
Repository
Service
API