DBContext cache expression tree of a query - entity-framework

I'm actually working on a project who use a very big linq to entity query (around 250 lines) who works with a lot of different entities.
This query depend of few parameters (3 or 4 max) so I think to "store" the expression tree of my query and modifying this one with just my few parameters like this.
IQueryable<Foo> myQuery =
GetBaseQuery()
.Where(a => a.Param1 == "foo")
.Where(a => a.Param2 == "bar");
Apart using compiled queries, is there a way to do that ?
This problem has some conditions
I use DbContext and changing to ObjectContext is difficialy possible and it seems that this is a pre requities for using compiled queries.
I use Visual Studio 2010 so... i'm limited to .NET 4 and entity framework 4.3 (no .NET 4.5, EF5 and autocompiled queries unhopefully)
So, Is there a way to
Store an expression tree who will be just used as a "base" and will not take too much time to be generated ?
Or using .NET 4.5 in visual studio 2010 ?
Or using Compiled queries with a DbContext
REALLY Thank's by advance !!!

Reading between the lines, it seems you want compiled queries with DbContext. (This isn't really the same as storing the LINQ expression tree, but that doesn't seem to be the performance bottleneck in the testing we have done anyway.) As far as I know the options you have are:
Use EF5 with .NET 4.5. Even if you are using VS2010 and targeting .NET 4 if you install .NET 4.5 on the machine where the app is run then you will get auto-compiled queries.
Use EF6 with .NET 4 or .NET 4.5. EF6 moves the auto-compiled query code out of the .NET Framework so it gets used even when running on .NET 4.
Use ObjectContext and CompiledQuery
I'm not aware of any way to use CompiledQuery with DbContext and there also aren't any plans to make this work.

Related

Is Entity Framework 6 used code first approach only?

Is Entity Framework 6 uses code first approach only ? I am a bit confused when we talk about code first approach, but i want to know if it works only with entity framework 6 or entity framework 6 uses code first approach only?
No, EF 6 does not ony support Code First. Code first can be a confusing name. It does not mean that you write code and then generate a db from that. You could in fact both target a new or an existing database using code first strategy. Code first simply imply not using the model designer (edmx) to create and maintain your db-context in your development project.
Some links to get you going:
EF 6 Database first (with asp.net mvc)
EF 6 Code first against new db (with asp.net mvc)
EF 6 Code first against existing db
Ps. Worth noting is that EF 7 (Core) will only support the Code First approach (but of course, as in EF 6, against a new or an existing db).
Checkout this links to find the Different approach of Entity Framework,
Basically we have 3 approach in that,
Code-First
Code First(Reverse Engineering) development targeting an existing
database.
simple-code-first-example
Model-First
model-first-with-entity-framework
Database-First
database-first-with-entity-framework

Entity Framework 6 with Sql Server Compact 3.5 SP2?

I have found lots of information about using Entity Framework 6 with Sql CE 4 and on using Entity Framework 4 with both Sql CE 4 and 3.5, but using Entity Framework 6 with Sql CE 3.5 is a different matter. As far as I can tell, CE 3.5 ships with a provider for Entity Framework 4 in System.Data.SqlServerCe.Entity.dll. This provider uses an older version of the base classes and if I try to use it, I get the following exception:
System.InvalidOperationException: The 'Instance' member of the Entity Framework provider type 'System.Data.SqlServerCe.SqlCeProviderServices, System.Data.SqlServerCe.Entity, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
This makes sense and points me to the realization that what I need is a provider for Entity Framework 6 for Sql Server CE 3.5 SP2. I can't seem to find such a thing though. The nuget package EntityFramework.SqlServerCompact seems to only reference Sql Compact 4 in all its versions. Does such a provider already exist? If so, where can it be found? I seem to be having difficulty locating it. If not, would it be realistic to think of implementing it by using the code in the one for CE 4 with a reference to 3.5, or is the difference between CE 3.5 and 4 too different for that to be straightforward? Is there another workaround?
I need to use 3.5 because of a need for using merge replication, which is not supported in 4, and would like to use Entity Framework. If there's not a straightforward answer to this, I'll probably use something other than Entity Framework.
OK, this is the second request I hear for this, let us do it. I will fork the code, and make a Nuget package for 3.5 - how about "EntityFramework.SqlServerCompact.Legacy" ? The "only" code change required is the fact, that 3.5 does not support ORDER BY..FETCH..OFFSET syntax (ie paging) called via Take and Skip. Would you be interested in this?
UPDATE: Package now available on NuGet http://www.nuget.org/packages/EntityFramework.SqlServerCompact.Legacy

Transaction wrapped around Enterprise Data Library code + Entity Framework code

Hey I have a unique and troubling situation.
I am working on a project where my team has used a mixture of Enterprise Data Library plus Entity Framework. Obviously this probably not recommended but it is what I'm stuck with. I would like to take a method written using Enterprise Data Library and also take a different method written using Entity Framework, and wrap both of these methods in a single transaction (without requiring Microsoft Distributed Transaction Service). I'm hoping to minimize rewriting code and be able to wrap the two methods in a single transaction just as they are. Is this possible? Thanks.
Using SQL Server 2008 and .NET 4.0
I suppose you want to mix EF and EntLib queries in the same TransactionScope, without using DTC.
Good news: it's possible. If you use exactly the same connection string for EntLib DAAB and EF DbContext, it will work, depending on the versions of EF, EntLib and SQL Server.
So what you have to do is:
check you have the right versions
get the version of your connection string changed by EF and use it instead of yours.
For 1, I have checked these combinations:
SQL Server must be 2008 or older
EntLib 4.1 + EF 5 doesn't work
EntLib 5 + EF 5 works
For 2, to get the connection string changed by EF you have to debug the execution of code that uses an instance of a DbContext. Set a breakpoint and watch or inspect the DbContext Database.Connection.ConnectionString property. That's the EF version of your connection string. Use it for both EF and EntLib and you'll get rid of DTC.

How do I precompile an Entity Framework Code-First Query?

I am encountering some performance problems with my Entity Framework Code-First queries and I believe that precompilation may be the answer. If I were using "normal" Entity Framework, I would simply use the CompiledQuery.Compile method to precomiple my queries. But since I have a DbContext and not an ObjectContext, I can't get this to work.
I do realize that DbContext is an IObjectContextAdapter, which gives me access to the ObjectContext, but I cannot find the method that lets me get an IQueryable from my object context that works in my precompiled query. I tried to use CreateObjectSet, but when EF tried to run the query it complained that it couldn't convert that method into SQL.
So what is the best way to precompile LINQ queries against a Code-First DbContext?
This will be probably solved in EFv4.2 EF vNext (currently in the very first CTP) by auto-compiled LINQ queries.
As from the official announecment:
"No compiled query support from DbContext
Unfortunately due to some technical limitations in the compiled query functionality we shipped in .NET Framework 4.0 we are unable to support compiled queries via the DbContext API. We realize this is a painful limitation and will work to enable this for the next release. "
Link.

entity framework with SP

I'm using VS 2008 with SP1. I want to use SP in the entity framework. The problem is that my SP returns more than 1 result set. How do I get the multiple result sets? All the online examples are showing single result. Please help me.
Entity Framework unfortunately does not support multiple result sets from stored procedures - not even in the .NET 4 release.
You will need to either rewrite your stored procs, or access them using standard, bare-bones ADO.NET - and ask Microsoft for support for multiple SP result sets in EF 5 !! I'll cast my vote in favor, too!
Related to this question:
Entity Framework - get records in multiple tables using stored procedure
Another SO user reports success with a plugin project, EF Extensions.
As marc_s describes, the feature is not built in to EF... another reason for DBA Developers to shy away from EF, imho.