有没有ApplyPropertyChanges有一个DataContext? [英] Is there a ApplyPropertyChanges with a datacontext?
本文介绍了有没有ApplyPropertyChanges有一个DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到使用entiyt框架的一个例子,它起诉ApplyPropertyChanges方法来更新的实体。有没有在普通的旧的LINQ到SQL的equivilent方法?
我的应用程序是有一个asp.net MVC应用程序,当我跑我的编辑操作我想只需要调用sometihng这样的:
originalObject = GetObject的(ID);
DataContext.ApplyPropertyChanges(originalObject,updatedObject);
DataContext.SubmitChanges();
解决方案
这个方法应该做你需要:
公共静态无效的应用< T>(T originalValuesObj,T newValuesObj,PARAMS字符串[]忽略)
其中T:类
{
//检查空参数
如果(originalValuesObj == NULL || newValuesObj == NULL)
{
(从originalValuesObj == NULL:来?)抛出新ArgumentNullException;
}
//工作变量(S)
类型类型= typeof运算(T);
名单<字符串> ignoreList =新的名单,其中,串>(忽略??新的String [] {});
//遍历每个对象上的属性
的foreach(的PropertyInfo PI在type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
//检查我们是否应该忽略这个属性
如果(ignoreList.Contains(pi.Name))
{
继续;
}
//设置在原始对象的值
反对toValue = type.GetProperty(pi.Name).GetValue(newValuesObj,NULL);
type.GetProperty(pi.Name).SetValue(originalValuesObj,toValue,NULL);
}
返回;
}
I have seen an example using the entiyt framework and it sued the ApplyPropertyChanges method to update an entity. Is there an equivilent method in plain old Linq-To-SQL?
My applicaiton is an asp.net MVC app and when I run my Edit action I want to just call sometihng like:
originalObject = GetObject(id);
DataContext.ApplyPropertyChanges(originalObject, updatedObject);
DataContext.SubmitChanges();
解决方案
This method should do what you require:
public static void Apply<T>(T originalValuesObj, T newValuesObj, params string[] ignore)
where T : class
{
// check for null arguments
if (originalValuesObj == null || newValuesObj == null)
{
throw new ArgumentNullException(originalValuesObj == null ? "from" : "to");
}
// working variable(s)
Type type = typeof(T);
List<string> ignoreList = new List<string>(ignore ?? new string[] { });
// iterate through each of the properties on the object
foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
// check whether we should be ignoring this property
if (ignoreList.Contains(pi.Name))
{
continue;
}
// set the value in the original object
object toValue = type.GetProperty(pi.Name).GetValue(newValuesObj, null);
type.GetProperty(pi.Name).SetValue(originalValuesObj, toValue, null);
}
return;
}
这篇关于有没有ApplyPropertyChanges有一个DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文