public T Update(T update) { }
目前对于这种方法我实现它就像这样......在Linq to SQL中还有其他简单的方法
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
}
这很好用,但是我希望我能改进这段代码...
_table是一个ITable