1

I'm receiving this error:

System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details. ---> System.Data.EntitySqlException: 'All' is a reserved keyword and cannot be used as an alias, unless it is escaped. Near line 1, column 1. at System.Data.Common.EntitySql.CqlLexer.MapUnescapedIdentifier(String symbol) at System.Data.Common.EntitySql.CqlLexer.MapIdentifierOrKeyword(String symbol) at System.Data.Common.EntitySql.CqlLexer.yylex() at System.Data.Common.EntitySql.CqlParser.yylex() at System.Data.Common.EntitySql.CqlParser.yyparse() at System.Data.Common.EntitySql.CqlParser.Parse(String query) at System.Data.Common.EntitySql.CqlQuery.Parse(String commandText, ParserOptions parserOptions) at System.Data.Common.EntitySql.CqlQuery.CompileCommon(String commandText, Perspective perspective, ParserOptions parserOptions, Func`3 compilationFunction) at System.Data.Objects.EntitySqlQueryState.Parse() at System.Data.Objects.ELinq.ExpressionConverter.TranslateInlineQueryOfT(ObjectQuery inlineQuery) at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

...< snip >...

at System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.Where(IQueryable1 source, Expression`1 predicate) at MyNamespace.All.GetEmptyList() in All.cs: line 35

which seems to be caused by the name of the test class being 'All'.

[TestClass]
public class All : Service
{
    [TestMethod]
    public void GetEmptyList()
    {
        var actualList = MyItems.Where(item => item.Id < 0);
    }
}

The MyItems property is a public property on the Service base class:

public IQueryable<MyItem> MyItems
{
    get { return Set<MyItem>(); }
}

I presume that my class name is being converted into an ESQL type, which turns it into a reserved word. However, I have no (direct) control over that, and as the ESQL and TSQL is not my concern, I don't think this problem should bubble-up, especially as I don't see what I can do about it, apart from rename my class (if I change it to 'All2', everything works fine.)

Why doesn't the expression parser (whereever specifically, maybe ExpressionConverter.Convert() or TranslateInlineQueryOfT) automatically escape reserved words?

Is there a way way I can fix this problem? Changing the class name is a hack.

(Running code-first with .NET 4, EF 4.1, against MS SQL 2008 R2.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
nicodemus13
  • 2,258
  • 2
  • 19
  • 31

1 Answers1

0

Actually changing the class name would be a huge aid to comprehensibility.

All what?

The entity framework is generic, where would it get a list of reserved words from and how. If there is a fix I suspect it will be an attribute to either escape or map the class name for use in the back end. It's the way I'd do it anyway.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • Within the wider context 'All' is clear, I've just left that out. I take your point, however, given that there is a provider (here the System.Data.SqlClient), this could implement the functionality. Furthermore, escaping by default would avoid such problems surely? It's one of annoying aspects of ORMs where they aren't storage-agnostic. Changing the db-backing to an in-memory collection doesn't raise the problem- which suggests the 'UNION' statement in ESQL itself isn't the problem, but the TSQL one. – nicodemus13 Nov 16 '11 at 12:15
  • Nah it's not TSQL, if you were going native it would have been [ALL], something in the ORM should be wrapping everything up in whatever the DBMS uses to wrap 'object' names – Tony Hopkinson Nov 16 '11 at 21:55
  • Changing the backing store to memory won't help here, I realised. If it's not the TSQL, then, it seems to me that it should be dealt with automatically (and probably even if it is TSQL). – nicodemus13 Nov 17 '11 at 09:59