我有一个基本 View ,它返回与表相同的列(给予或获取 1 个字段)
在我的 DAL 代码中,我返回 MyTableObject 的列表,但在某些情况下,我将调用 View 来返回相同的数据,但来自不同的源。
List<MyTableObject> tableObjects = new List<MyTableObject>();
if (case1)
tableObjects = entities.MyTableObjects.Where(criteria).ToList();
else
tableObjects = entities.MyViewObjects.Where(criteria).ToList(); // <-- This will obviously break
return tableObjects;
有没有办法将映射 View 实体作为表实体返回? (除了让表和 View 实现相同的接口(interface)并返回该接口(interface)之外)我想将返回类型保留为 MyTableObject。
我遇到了Auto Mapper ,但不确定它是否适合这种情况..
最佳答案
看起来我找到了一个很酷的解决方案..
最初我尝试实现接口(interface)方法并遇到一些casting issues (与我的谓词构建器一起使用接口(interface)),并且接口(interface)必须为实现该接口(interface)的每个实体创建部分类..
答案..POCO。
我用过Poco Template for EF ,而不是简单地编辑 xxxPocoGenerator.Context.tt 以从 MyViews 集合返回 MyTable 对象(一行)。
public ObjectSet<Trade> v_Trade {
get { return _v_Trade ?? (_v_Trade = CreateObjectSet<Trade>("Trades")); }
}
又好又简单..
关于linq - Entity Framework ,将 View 映射到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3992630/