Entity Framework 4.0: Entity SQL CAST Operation No

2019-05-08 06:22发布

问题:

I'm trying to make a query where I cast a text column, which contains an integer as text, to an Int32. Here's the query:

SELECT VALUE t FROM MyEntities AS t WHERE CAST(t.TextColumn AS Edm.Int32) > 5

However, I get an System.Data.EntitySqlException with the following message:

Type 'Edm.Int32' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly. Near type name, line 1, column 75.

According to MSDN, Edm.Int32 should be a valid type.

Does anyone know what's wrong?

Edit:

After some trial and error, I found that the following works:

SELECT VALUE t FROM MyEntities AS t WHERE CAST(t.TextColumn AS System.Int32) > 5

Are the examples in MSDN wrong? I feel like I'm missing something here...

回答1:

If a query is executed with the EntityCommand, the data type is an EDM type, while if the query is executed with ObjectQuery, the data type is a CLR type.

Looks like you are executing the comman via the ObjectQuery.