kumar
kumar

Reputation: 1127

Compiled Query error: there is no implicit conversion from entities to 'System.Data.Objects.ObjectContext

I'm creating a delegate to retrieve all customers records from database. I have used compiled query in this way , but for some reason I'm getting an error like this in Visual studio 2012 with EF.

Error: The type 'HTML5Basics.NorthwindDataContext' cannot be used as type parameter 'TArg0' in the generic type or method 'System.Data.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)'. There is no implicit reference conversion from 'HTML5Basics.NorthwindDataContext' to 'System.Data.Objects.ObjectContext'.

What is this error and how to solve this error?

Here is code:

public static Func<NorthwindDataContext, string, IEnumerable<SimpleCustomer>> CustomersByCity =
            CompiledQuery.Compile<NorthwindDataContext, string, IEnumerable<SimpleCustomer>>(
            (NorthwindDataContext db, string city) =>
            from c in db.Customers
            where c.City == city
            select new SimpleCustomer { ContactName = c.ContactName });

Upvotes: 3

Views: 1512

Answers (1)

Stefan
Stefan

Reputation: 17658

There is no implicit reference conversion from 'HTML5Basics.NorthwindDataContext' to 'System.Data.Objects.ObjectContext'.

Indicates there is no conversion between the two types.

In .NET 4.5, EF5 there is a System.Data.Objects namespace which contains CompiledQuery.Compile function. There is also one in the System.Data.Linq namespace.

They have a different signature:

System.Data.Linq Namespace: (taken from MSDN http://msdn.microsoft.com/en-us/library/bb548737.aspx):

public static Func<TArg0, TResult> Compile<TArg0, TResult>(
Expression<Func<TArg0, TResult>> query)
where TArg0 : DataContext

System.Data.Objects Namespace (from .pdb):

public static Func<TArg0, TResult> Compile<TArg0, TResult> 
(Expression<Func<TArg0, TResult>> query) 
where TArg0 : ObjectContext

Basically you have 2 choices:

1) use the one in the System.Data.Linq namespace.

2) pass in an ObjectContext (or inherited type) into the System.Data.Objects namespace's version.

Upvotes: 1

Related Questions