实体框架中的EntitySqlException

时间:2014-09-27 19:33:41

标签: .net entity-framework orm entity-framework-4

我正在阅读Programming Entity Framework, 2nd Edition本书,该示例使用Entity SQL。更改示例中的上下文名称并更改行

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 个答案:

答案 0 :(得分:1)

你错过了

之后的空间
"SELECT VALUE c "