Sunday, 15 March 2015

c# - A simple Where clause stopped working in EF5 -



c# - A simple Where clause stopped working in EF5 -

i had several queries 2 examples below:

// illustration 1: var dataseries = (from d in dataseries d.symbol == symbol select d).firstordefault(); // illustration 2: homecoming markets.where(m => m.dataseries == dataseries).toarray();

which working fine until ran "update model database...". now, i'm getting notsupportedexception:

unable create constant value of type 'mytest.symbol'. primitive types or enumeration types supported in context.

yes, verified symbol , d.symbol (and m.dataseries , dataseries) of same types.

and yes, can alter query utilize p/f key relationships so:

var dataseries = (from d in dataseries d.symbol.id == symbol.id select d).firstordefault();

but don't want have alter code p/f key relationships when object relationships working fine.

question: how first examples working again?

you can't write where clause in linq query executed against database. remember code will translated sql , run on database engine.

so, first example, how entity framework know how compare 2 instances of complex type mytest.symbol? ef can't, because there no suitable sql such comparison.

you must specify in where clause db engine understand, sql where clause based on id in primitive type (int, bigint,...).

you could fetch info database using .tolist(), , apply where clause on in-memory resulting list, where clause won't have translated sql:

markets.tolist().where(m => m.dataseries == dataseries);

but lose benefits of db server: huge usage of memory (everything loaded in context), poor performances....etc.

you should execute where against db, means you'll have utilize primitive types comparison:

markets.where(m => m.dataseries.id == dataseries.id);

c# entity-framework entity-framework-5 where-clause

No comments:

Post a Comment