实体框架,将视图映射到表 [英] Entity Framework, mapping Views to Tables

查看:81
本文介绍了实体框架,将视图映射到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本视图,该视图返回与表相同的列(给定或取1个字段)

I have a basic view that returns the same columns as a table (give or take 1 field)

在我的DAL代码中,我将返回MyTableObject的列表,但是在某些情况下,我将调用该视图以返回相同的数据,但来自不同的源.

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;

有没有一种方法可以将Map视图实体作为表实体返回? (除了让表和视图实现相同的接口并返回该接口外),我想将返回类型保持为MyTableObject.

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.

我遇到了 Auto Mapper ,但是不确定是否适合这种情况.

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..

答案.. POCO .

已使用

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")); }
}

又好又简单.

这篇关于实体框架,将视图映射到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆