EF retrieve entities from undefined table/view
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 the开发者_运维技巧re 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
精彩评论