#region 修改
/// <summary>
/// 修改一条数据
/// </summary>
/// <param name="model">修改的对象</param>
/// <param name="propertys">需要修改的属性</param>
/// <returns>影响行数</returns>
public int UpdateByModel(T model, params string[] propertys)
{
DbEntityEntry<T> entry = db.Entry(model);
entry.State = EntityState.Unchanged;
foreach (string property in propertys)
{
entry.Property(property).IsModified = true;
}
db.Configuration.ValidateOnSaveEnabled = false;
return db.SaveChanges();
}
/// <summary>
/// 修改多条数据
/// </summary>
/// <param name="modelList">修改的对象的集合</param>
/// <param name="propertys">需要修改的属性</param>
/// <returns>影响行数</returns>
public int UpdateByList(List<T> modelList, params string[] propertys)
{
if (propertys == null)
{
throw new ArgumentNullException("属性名不能为空");
}
modelList.ForEach(m =>
{
DbEntityEntry<T> entry = db.Entry(m);
entry.State = EntityState.Unchanged;
foreach (string property in propertys)
{
entry.Property(property).IsModified = true;
}
db.Configuration.ValidateOnSaveEnabled = false;
});
return db.SaveChanges();
}
/// <summary>
/// 根据条件更新
/// </summary>
/// <param name="where">条件</param>
/// <param name="model">修改的对象</param>
/// <param name="propertys">需要修改的属性</param>
/// <returns>影响行数</returns>
public int UpdateByWhere(Expression<Func<T, bool>> where, T model, params string[] propertys)
{
if (where == null)
{
throw new ArgumentNullException("条件(where)不能为空");
}
List<T> query = db.Set<T>().Where(where).ToList();
Type t = typeof(T);
//获取公共属性
List<PropertyInfo> propertyInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
//生成字典
Dictionary<string, PropertyInfo> dicPropertys = new Dictionary<string, PropertyInfo>();
propertyInfos.ForEach(p =>
{
if (propertys.Contains(p.Name))
{
dicPropertys.Add(p.Name, p);
}
});
//修改已查询出的对象中需要修改的属性的值
foreach (string property in propertys)
{
if(dicPropertys.ContainsKey(property))
{
PropertyInfo proInfo = dicPropertys[property];
object newValue = proInfo.GetValue(model, null);
foreach (T item in query)
{
proInfo.SetValue(item, newValue, null);
}
}
}
return db.SaveChanges();
}
#endregion
更新比较复杂一点点
static void Main(string[] args)
{
try
{
MT5RiskManagementEntities mt5 = new MT5RiskManagementEntities();
EntityFramework<User> ef = new EntityFramework<User>(mt5);
int count = 0;
User user = new User() { id = 104, name = "雷军", age = 48 };
count = ef.UpdateByModel(user, "name", "age");
Console.WriteLine(string.Format("更新了{0}条数据", count));
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
static void Main(string[] args)
{
try
{
MT5RiskManagementEntities mt5 = new MT5RiskManagementEntities();
EntityFramework<User> ef = new EntityFramework<User>(mt5);
int count = 0;
List<User> users = new List<User>();
users.Add(new User() { id = 104, name = "马云", age = 53 });
users.Add(new User() { id = 105, name = "马云", age = 53 });
count = ef.UpdateByList(users, "name", "age");
Console.WriteLine(string.Format("更新了{0}条数据", count));
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
static void Main(string[] args)
{
try
{
MT5RiskManagementEntities mt5 = new MT5RiskManagementEntities();
EntityFramework<User> ef = new EntityFramework<User>(mt5);
int count = 0;
User user = new User() { age = 53 };
count = ef.UpdateByWhere(u => u.id > 140, user, "name", "age");
Console.WriteLine(string.Format("更新了{0}条数据", count));
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}