我想找到一种解决方法来完成一个简单的解决方案,以便通过 EF 自动执行某些操作。
我需要的是在保存和检索过程中接管以修改查询结果,但此类将能够使其适用于任何类型的实体。
示例:我有一个 MyTestDb。因此,在我的 C# 项目中,我创建了一个新的实体模型 (MyTEstDbModel.edmx),并生成相对的 POCO 类。
好吧,一个有趣的点可能是实现一个新的自定义类,如下所示:
class Example
{
private ObjectContext _context;
private Example(ObjectContext obj) { _context = obj; }
public void Store(ObjectSet<???generic???> os)
{
// problem here: I dont't know the type contained in ObjectSet
// but if I Knew its type, I could make a work like this:
// -> foreach every instance in objectSet to check if exist some property
// via reflection, if i found them, then I set always the same values.
// Why this? Because all my db contains some common filed
// like (createdByUser, TimeToUpdate, and so on..), so it wold be boring
// setting all those fileds from any point of program.
}
public void Retrive(ObjectSet<???generic???> os)
{
// here problem too: all my queries will be filtered by one or more value
// fileds, but I cannot use lambaExpression cos I don't Know the type
// contained in objectSet<..>
}
//....
最后,在程序的任何一点,代码都应如下所示:
Example obj = new Example(myEntityContext); //-> global
var result = myEntityContext.ObjectSetTyped.Where(..lambaExpression..condition)
result.someProperty = "...";
obj.Store(result); // store method will fill all other boring filed automatically.
谁能给我一些关于我的问题的提示、帮助和建议?
提前致谢...
Update
现在,只剩下另一个问题了。我将通过检索方法过滤我的 ObjectSet,如下所示:
public void Retrieve<TEntity>(IQueryable<TEntity> ooo) where TEntity : IC
{
ooo = ooo.Where(p => p.Filed == "MyDefaultValue");
}
但是,从外部方法来看,objectSet 结果不受我的过滤器的影响。
为何如此..?
MyEntities ent = new...
MyWrapper wrap = new MyWrapper();
wrap.Retrieve(ent.Users);
//这里的问题->用户objectSet总是相同的..