映射的问题
1 前端传一个EntityDto,我这边接收
现在的问题是.想根据里面的主键Id,先是找出这条数据(假设存在)
那我找到了这条数据之后,如何将剩余的属性值一一赋值
只想到了一个比较笨的方法
QueryEntity(查询出来的那条数据)
QueryEntiy.xxx1=EntityDto.xxx1;
QueryEntity.xxx2=EntityDto.xxx2;
QueryEntity.xxx3=EntityDto.xxx3
还有一种是用automap方法
var QueryEntity=_maper.Map<Entity>(EntityDto);
这种的话可以一次性赋值,但是去更新会报错,或者直接就在数据库插入一条新的数据了(在底层写个扩展方法)
/// <summary> /// /// </summary> /// <param name="entity"></param> /// <param name="selectKey">筛选列</param> /// <param name="updateKey">需要更新列</param> public void AddUpdate(T entity, string[] selectKey, string[] updateKey) { if (entity != null) { var model = dbset.Where(EfUtils.And<T>(selectKey, entity)).FirstOrDefault(); if (model == null) { foreach (var item in entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (item.Name == "CreateTime") { var val = System.DateTime.Now; entity.GetType().GetProperty(item.Name).SetValue(entity, val, null); } } dbset.Add(entity); } else { if (updateKey.Length == 1 && updateKey[0] == "*") { foreach (var item in entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { // Id为主键不能更新,否则报错,可根据实际主键设定,createtime系统自动设定 if (item.Name != "Id" && item.Name != "CreateTime") { var val = entity.GetType().GetProperty(item.Name).GetValue(entity, null); if (item.Name == "ModifyTime") { val = System.DateTime.Now; } model.GetType().GetProperty(item.Name).SetValue(model, val, null); } } } else { foreach (var k in updateKey) { if (k != "Id" && k != "CreateTime") { var val = entity.GetType().GetProperty(k).GetValue(entity, null); if (k == "ModifyTime") { val = System.DateTime.Now; } model.GetType().GetProperty(k).SetValue(model, val, null); } } } dbset.Attach(model); } } }
public static class EfUtils { public static Expression<Func<T, bool>> And<T>(string[] keys, T t) { if (keys == null) return null; Expression<Func<T, bool>> express = t => true; foreach (var k in keys) { var obj = t.GetType().GetProperty(k); var val = obj.GetValue(t, null).ToString(); var type = obj.ToString(); if (k.ToLower().Equals("datetime") || k.ToLower().Equals("clientdatetime")) express = express.And(AndIndexOf<T>(type, k, val)); else express = express.And(And<T>(type, k, val)); } return express; } public static Expression<Func<T, bool>> AndIndexOf<T>(string type, string key, string val) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); Expression left = Expression.Property(candidateExpr, typeof(T).GetProperty(key)); var cme = typeof(string).GetMethod("Contains"); var ce = Expression.Constant(val); var call = Expression.Call(left, cme, ce); return Expression.Lambda<Func<T, bool>>(call, candidateExpr); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> exp_left, Expression<Func<T, bool>> exp_right) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(exp_left.Body); var right = parameterReplacer.Replace(exp_right.Body); var body = Expression.And(left, right); return Expression.Lambda<Func<T, bool>>(body, candidateExpr); } public static Expression<Func<T, bool>> And<T>(string type, string key, string val) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); Expression left = Expression.Property(candidateExpr, typeof(T).GetProperty(key)); Expression right = null; if (type.ToLower().IndexOf("system.nullable`1[system.int32]") >= 0) right = Expression.Constant(Convert.ToInt32(val), typeof(int?)); else if (type.ToLower().IndexOf("int32") >= 0) right = Expression.Constant(Convert.ToInt32(val), typeof(int)); else right = Expression.Constant(val); Expression filter = Expression.Equal(left, right); return Expression.Lambda<Func<T, bool>>(filter, candidateExpr); } }
internal class ParameterReplacer : ExpressionVisitor { public ParameterReplacer(ParameterExpression paramExpr) { this.ParameterExpression = paramExpr; } public ParameterExpression ParameterExpression { get; private set; } public Expression Replace(Expression expr) { return this.Visit(expr); } protected override Expression VisitParameter(ParameterExpression p) { return this.ParameterExpression; } }