1

I am trying to follow a textbook tutorial, I create the model but I don't know why I cannot reference to it. Anyway, in collusion when I try to read the data from the database I get an exception, saying that the table was not found. Thank you for any help and advice.

My Code:

using System.Collections.Generic; 
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Data.Common;
using System.Data;
using System.Configuration;
//using StockHistoryModel; This is my model and is not available.

static void Main(){
  string connectionString = ConfigurationManager.ConnectionStrings
    ["StockHistoryEntities"].ConnectionString;
  EntityConnection connection = new EntityConnection(connectionString);
  connection.Open();
  EntityCommand command = new EntityCommand("SELECT SymbolID FROM Shares",    
    connection);
  DbDataReader reader = command.ExecuteReader(
    CommandBehavior.SequentialAccess);
  while (reader.Read())
    Console.WriteLine("{0}", reader.GetValue(0));
   }
}

Exception:

enter image description here

My Model:

enter image description here

Doro
  • 671
  • 1
  • 16
  • 34
  • Did you try executing query 'SELECT SymbolID FROM Shares' in SSMS? I mean in sql? – Jenish Rabadiya Feb 21 '15 at 12:50
  • Show your connection string – Jenish Rabadiya Feb 21 '15 at 12:57
  • This is the connection: – Doro Feb 21 '15 at 13:04
  • When you work with `EntityCommand`, you have to use ESQL instead of SQL. Try `"SELECT VALUE SymbolID FROM yourNamespace.Shares"` (tell me what is your namespace in your edmx) – Khanh TO Feb 21 '15 at 13:12
  • I just tried but still I get the exception. But why I cannot referent the namespace with using? – Doro Feb 21 '15 at 13:19
  • edmx namespace and c# namespace are different. You cannot use `using`. `using` is for c# namespace. – Khanh TO Feb 21 '15 at 13:42
  • Ok, but why in my textboox shows to import with using? Anyway, as long as I make it work I don't mind. – Doro Feb 21 '15 at 13:59
  • your `using` is used by c# compiler at compilation time. But your code is at runtime, you need to specify a namespace.Try: `"SELECT VALUE SymbolID FROM StockHistoryModel.Shares"` (assuming that `StockHistoryModel` is your namespace) – Khanh TO Feb 21 '15 at 14:07
  • I realized I had a mistake. Please try: `"SELECT VALUE Shares.SymbolID FROM StockHistoryModel.Shares as Shares"` (assuming that `StockHistoryModel` is your namespace) – Khanh TO Feb 21 '15 at 14:39
  • Still not working :-( – Doro Feb 21 '15 at 14:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71406/discussion-between-doro-and-khanh-to). – Doro Feb 21 '15 at 20:44

0 Answers0