EF从未定义的表/视图检索实体 [英] EF retrieve entities from undefined table/view

查看:210
本文介绍了EF从未定义的表/视图检索实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从模型中未定义的视图中检索记录,并使用列名或序号检索列值?



我写这段代码:

  AppContext ctx = new AppContext(name = DbConnString); 
string commandText =SELECT V.ID,V.Code,V.Description FROM vw_UserDefinedView AS V;
ObjectQuery< DbDataRecord> query = new ObjectQuery< DbDataRecord>(commandText,ctx);

但是当我尝试执行它时发生异常:

 'vw_UserDefinedView'无法在当前范围或上下文中解析。确保所有引用的变量都在范围内,所需的模式被加载,并且该命名空间被正确引用。接近简单的标识符,第1行,第43列。

有没有办法使用Entity Framework完成此任务ObjectContext(或DbContext)?



最好的问候,
Alberto

解决方案>

不,这是不可能的。消息已经声明:'vw_UserDefinedView'无法在当前范围或上下文中解决。该视图对于上下文(.edmx)是不知道的。你必须意识到你正在查询实体模型,而不是数据库!



如果你不想在你的实体模型中看到这个视图(无论什么原因),那么只需使用 SqlCommand SqlConnection SqlDataReader 执行您的语句关于vw_UserDefinedView。



更新



也许此链接可以帮助您进一步:实体框架:在运行时添加属性/实体


Is it possible to retrieve records from a view that has not been defined in the model and to retrieve his columns value by using column name or ordinal ?

I write this code :

AppContext ctx = new AppContext("name=DbConnString");
string commandText = "SELECT V.ID, V.Code, V.Description FROM vw_UserDefinedView AS V";
ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(commandText, ctx);

but an exception occurred when I try to execute it :

'vw_UserDefinedView' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 1, column 43.

Is there a way to accomplish this using Entity Framework and ObjectContext (or DbContext) ?

Best regards, Alberto

解决方案

No, this is not possible. As the message already states: "'vw_UserDefinedView' could not be resolved in the current scope or context." This view is not known to the context (.edmx). You have to realize that you are querying the Entity Model and not the Database!

If you don't want this view (for whatever reason) in your Entity Model, then simply use SqlCommand, SqlConnection and SqlDataReader to execute your statements concerning vw_UserDefinedView.

UPDATE

Maybe this link can help you further: Entity Framework : Add Properties/Entities during runtime

这篇关于EF从未定义的表/视图检索实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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