Mig*_*ura 4 c# entity-framework
我有以下方法:
public void Update<T>(T entity, IEnumerable<Expression<Func<T, Object>>> properties)
where T : class
{
_context.Set<T>().Attach(entity);
foreach (var property in properties)
_context.Entry<T>(entity)
.Property(((MemberExpression)property.Body).Member.Name)
.IsModified = true;
} // Update
Run Code Online (Sandbox Code Playgroud)
我正在传递一个Entity Framework实体,附加它并将每个属性设置为已修改.
我想用它如下:
_repository.Update<File>(file, new { x => x.Data, x => x.Name });
Run Code Online (Sandbox Code Playgroud)
所以我传递一个文件并说数据和名称属性被修改.
但是我收到了错误:
The best overloaded method match for 'Update<File>(File,
IEnumerable<System.Linq.Expressions.Expression<System.Func<File,Object>>>)'
has some invalid arguments
Run Code Online (Sandbox Code Playgroud)
如上所述,我应该如何更改我的方法才能使用它?
或者可能:
_repository.Update<File>(file, x => x.Data, x => x.Name);
Run Code Online (Sandbox Code Playgroud)
甚至:
_repository.Update<File>(file, x => new { x.Data, x.Name });
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 12
看起来你真的想要:
public void Update<T>(T entity, params Expression<Func<T, Object>>[] properties)
where T : class
Run Code Online (Sandbox Code Playgroud)
然后将其称为:
_repository.Update(file, x => x.Data, x => x.Name);
Run Code Online (Sandbox Code Playgroud)
(注意我在这里使用的是类型推断而不是显式使用_repository.Update<File>(...).)
该params部分是如何指定要转换为数组的多个参数.根本不需要匿名类型.如果你真的想要一个匿名类型,你可以通过反射访问它的成员 - 但这将是非常难看的,我怀疑你也需要转换每个lambda表达式(否则编译器将无法推断其类型) .
| 归档时间: |
|
| 查看次数: |
7107 次 |
| 最近记录: |