【发布时间】:2019-04-09 23:30:32
【问题描述】:
我正在使用实体框架和存储库模式与数据库进行交互。
为简单起见,我正在做这样的事情。
public T Update(T entity)
{
// Update Entity
}
我想要做的不是更改函数外部的实体,而是希望能够传入表达式来更新对象。
public T Update(T entity, ItemINeedPassedIn, Expression<Func<TDBTable, bool>> predicate)
{
var dbEntity = await GetOneAsync(predicate); // Which fetches me the entity to change
// Code to attach the property value to entity goes here <-- This is what I need
// Update Entity
}
例如
更新(客户,x => x.FirstName = "John",x => x.Id == 4);
Customer 将为 null,这需要查找。这部分有效。
我需要将客户的名字更新为 john,其中 Id == 4。 我想传入表达式并将其附加到要更新的 dbEntity。
x => x.FirstName = "约翰"
应该以某种方式变成
dbEntity.FirstName = "约翰"
我该怎么做?
【问题讨论】:
-
什么是
ItemINeedPassedIn?为什么将 4 分配给 Id?应该是x.Id == 4? -
ItemINeedToPassIn 是我正在寻找和更新的 Id == 4
-
我觉得问题是:
ItemINeedPassedIn和dbEntity的类型是什么?或者,您在致电Update之前不认识他们吗?您知道谓词中x的类型吗?我认为,最简单的回答方法是让你给我们一个输入和一个期望的输出,同时澄清类型是否已知。 -
你真的需要
Expression<Func<TDBTable, bool>>,还是只需要Func<TDBTable, bool>?
标签: c# object reflection expression entity