C# 存储库中的通用编辑
目前对于这个方法,我是这样实现的..在Linq到SQL中还有其他简单的方法吗C# 存储库中的通用编辑,c#,generics,repository,edit,C#,Generics,Repository,Edit,目前对于这个方法,我是这样实现的..在Linq到SQL中还有其他简单的方法吗 public T Update(T update) { } 这工作很好,但我希望如果我可以改善这段代码请 _表是一个ITable抱歉,编辑。GetType是更新。GetType,我的意思是编辑和更新是相同的变量,但不知何故,我似乎无法在此处更改它…以更正错误code@user182630只需单击“编辑”并开始编辑:试过了,不让我 public T Update (T update) { var table =
public T Update(T update) { }
这工作很好,但我希望如果我可以改善这段代码请
_表是一个ITable抱歉,编辑。GetType是更新。GetType,我的意思是编辑和更新是相同的变量,但不知何故,我似乎无法在此处更改它…以更正错误code@user182630只需单击“编辑”并开始编辑:试过了,不让我
public T Update (T update)
{
var table = (IEnumerable<T>)(_table.AsQueryable());
var store = (T)((from a in table.Where(
a => a.GetType().GetPrimaryKey() ==
update.GetType().GetPrimaryKey()) select a).Single());
PropertyInfo[] properties = store.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
Object propertyValue = null;
if (null != property.GetSetMethod())
{
PropertyInfo entityProperty =
update.GetType().GetProperty(property.Name);
if (entityProperty.PropertyType.BaseType ==
Type.GetType("System.ValueType") ||
entityProperty.PropertyType ==
Type.GetType("System.String"))
propertyValue =
update.GetType().GetProperty(property.Name).GetValue(update, null);
if (null != propertyValue)
property.SetValue(store, propertyValue, null);
}
}
_context.submitChanges
return update
}