Search code examples
linq-to-sqlentity-frameworklinq-to-entitiescsla

LINQ2SQL switch to EF on reading a list of records


I am trying to switch from LINQ2SQL to EF ... I am getting the following error with some code that originally worked with LINQ2SQL and seems to compile correctly:

Csla.DataPortalException: DataPortal.Fetch failed (LINQ to Entities does not recognize the method 'MyApp.Logic.UserInfo FetchUserInfo(MyApp.Data.User)' method, and this method cannot be translated into a store expression.)

---> Csla.Reflection.CallMethodException: DataPortal_Fetch method call failed

---> System.NotSupportedException: LINQ to Entities does not recognize the method 'MyApp.Logic.UserInfo FetchUserInfo(MyApp.Data.User)' method, and this method cannot be translat...

This is the code:

var data = query.Select(row => UserInfo.FetchUserInfo(row));

this.AddRange(data);

I'm trying to read a list of data and load the entities into my class. I'm new to EF and just think I am overlooking something.

Any help would be appreciated!

For those interested, the solution was:

var data = query.AsEnumerable().Select(UserInfo.FetchUserInfo);

Solution

  • As far as I can see the problem is that Linq to Entities provider knows nothing about how to translate you custom method FetchUserInfo to ESQL.

    If UserInfo is just a DTO and UserInfo.FetchUserInfo is a kind of Entity to DTO conversion method this would help

    var data = query.AsEnumerable().Select(row => UserInfo.FetchUserInfo(row));
    

    .AsEnumerable() invoke will result in materialization of query results to memory objects.