Class name is a reserved word when query provider compiles ELINQ query to ESQL

500 Views Asked by At

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.)

1

There are 1 best solutions below

3
On

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.