问题描述
我有一个基本视图,该视图返回与表格(给或拿1个字段)
的相同列在我的DAL代码中,我正在返回MyTableObject的列表,但是在某些情况下,我会调用视图以返回相同的数据,但要从不同的来源返回.
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;
有没有办法映射视图实体作为表实体返回? (除了拥有表和查看实现相同的接口并返回该接口外)我想将返回类型保留为mytableObject.
我遇到自动映射器,但不确定是否适合这种情况. p>
推荐答案
看起来我找到了一个很酷的解决方案.
最初,我尝试实现接口方法并遇到一些铸造问题构建器),以及与接口必须为实现接口的每个实体创建部分类别的接口.
答案.. pocos .
ef 的POCO模板,而不是简单地编辑xxxpocogener.context.tt从myviews Collection(一行)返回mytable对象.
public ObjectSet<Trade> v_Trade { get { return _v_Trade ?? (_v_Trade = CreateObjectSet<Trade>("Trades")); } }
良好而轻松..
其他推荐答案
您可以编写存储过程(或 commandtextext 在模型中,不创建db对象),它将简单地调用"从视图中选择 *".然后创建函数import import import 为此过程并将返回类型设置为mytableObject.
问题描述
I have a basic view that returns the same columns as a table (give or take 1 field)
in my DAL code, i am returning a list of MyTableObject, however in some cases, i will call the view to return the same data, but from different sources.
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;
is there a way to Map view entities to be returned as table entities? (other than having table and view implement the same interface and return that interface) i would like to keep the return type as MyTableObject.
I came across Auto Mapper, but not sure if it would be suitable for this scenario..
推荐答案
Looks like i found a cool solution to this..
Initially I tried to implement interface approach and run into some casting issues (using interfaces alongside my predicate builder), and also with interfaces having to create partial classes for each entity that implement the interface..
the answer.. POCOs.
Iused Poco Template for EF, and than simply edited xxxPocoGenerator.Context.tt to return MyTable object from MyViews collection (one line).
public ObjectSet<Trade> v_Trade { get { return _v_Trade ?? (_v_Trade = CreateObjectSet<Trade>("Trades")); } }
nice and easy..
其他推荐答案
You can write a stored procedure (or CommandText in the model, without creating DB Object) that will simply call "Select * from View". Then create Function Import for this procedure and set the return type to MyTableObject.