2014-09-27 50 views
0

我正在通過Programming Entity Framework, 2nd Edition書,該示例使用Entity SQL。更改本例中的上下文的名稱並改變線路實體框架中的EntitySqlException

ObjectQuery<Contact> contacts = context.CreateQuery<Contact>(queryString); 

ObjectQuery<Contact> objectQuery = ((IObjectContextAdapter)context).ObjectContext.CreateQuery<Contact>(QUERY_STRING); 

我得到這個:

using (var context = new PROGRAMMINGEFDBEntities()) 
{ 
    const string QUERY_STRING = "SELECT VALUE c" + 
           "FROM PROGRAMMINGEFDBEntities.Contacts AS c " + 
           "WHERE c.FirstName='Robert'"; 
    ObjectQuery<Contact> objectQuery = ((IObjectContextAdapter)context).ObjectContext.CreateQuery<Contact>(QUERY_STRING); 
    foreach (var contact in objectQuery) 
    { 
     Console.WriteLine("{0} {1}", contact.FirstName, contact.LastName); 
    } 
} 

當它運行時,我得到以下異常:

System.Data.Entity.Core.EntitySqlException was unhandled 
    HResult=-2146232006 
    Message=The query syntax is not valid. Near identifier 'PROGRAMMINGEFDBEntities', line 1, column 20. 
    Source=EntityFramework 
    Column=20 
    ErrorContext=identifier 'PROGRAMMINGEFDBEntities', line 1, column 20 
    ErrorDescription=The query syntax is not valid. 

回答

1

您a重新丟失空格後

"SELECT VALUE c " 
+0

賓果...謝謝。 – BanksySan 2014-09-27 21:10:30