0

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

1 Answers1

0

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

Community
  • 1
  • 1
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • This view is not present in the Entity Model because it is supplied at runtime by the user and even the columns are user defined. The reason why I want to use the entity framework is mainly because I don't want to manage multiple connection strings and keep the database independence (SQL Server, Oracle and so on). Is there any other way to accomplish what I'm trying to do ? Is there a way to add a new entity definition to the model at runtime ? – Alberto Jun 14 '11 at 09:17
  • Don't know if you could add entity def. at runtime. – Youp Bernoulli Jun 14 '11 at 09:38
  • No, you're trying to take a screwdriver and use it as a knife. EF is simply not designed to do this. – Tridus Jun 14 '11 at 09:57