Transaction wrapped around Enterprise Data Library code + Entity Framework code - entity-framework

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.

Related

DBContext cache expression tree of a query

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.

Entity Framework: what do I do with edmgen.exe's output?

I'm forced to use a legacy SQL Server 2000 database for an EF-based app I am writing. The tables already exist, so I need to generate the Entities layer. I can do this in VS2010 using MySQL and recent versions of SQL Server, but not 2000.
To get around this, I followed some tutorials that explain how to generate csl, msdl and ssdl files using edmgen.exe.
That works fine. I now have those files in e.g. c:\temp.
Please can someone tell me what to do with these files? I want to Entity Framework-ify a simple console application that I have written. Can I somehow create an 'ADO.NET Entity Data Model' from these files so I end up with what I would have if I had used VS2010 all along?
Thanks
I got it working, here is what I did
1) Use edmgen.exe to generate the files mentioned in my question
"C:\windows\Microsoft.NET\Framework\v4.0.30319\edmgen.exe" /mode:fullgeneration /c:"Data Source=<your_server_here>; Initial Catalog=<your_catalog_here>; UID=<username>;PWD=<password>" /project:<vs2010_project_name> /entitycontainer:<project_name>Entities /namespace:<project_name>Model /language:CSharpEntityFramework
2) Follow these instructions "How To: use your existing CSDL/MSL/SSDL files in the Entity Designer CTP2" - its actually for VS2008 but it worked for me too.
http://blogs.msdn.com/b/dsimmons/archive/2007/12/07/how-to-use-your-existing-csdl-msl-ssdl-files-in-the-entity-designer-ctp2.aspx
3) I had a further problem in that my legacy db had no primary keys so I followed these instructions which also worked
http://pratapreddypilaka.blogspot.in/2012/04/entity-framework-adding-datatable-with.html
These files are referenced in EF's connection string and EF use them at runtime to build a mapping but it can still not work because SQL Server 2000 is not supported by EF (at least EF 4.0 and newer). EF can generate SQL not supported by SQL Server 2000 and you will get exceptions at runtime.

Does Microsoft Jet DB work with Entity Framework?

In this forum here , someone mentions that Entity Framework does not work with Access (Jet DB - .mdb). However it seems that there is a provider for Jet DB as described here
Which makes me think that the only thing I need with Entity Framework is to define the follwing before I define the models:
<connectionStrings>
<add name="ProductContext"
providerName="Microsoft.Jet.OLEDB.4.0"
connectionString="Source=C:\mydatabase.mdb;Jet OLEDB:Database
Password=MyDbPassword;"/>
</connectionStrings>
Does anyone know if Entity Framework works fine with Jet DB, I want to make sure it does before I start since my design document depends on this fact.
Thanks
Entity Framework does not support OLEDB connections, so your connection string will not work. It is practically impossible to get Entity Framework to collaborate with MS Access. You will either need to dump the MS Access part of your design, or the Entity Framework part.
The closest you could get using MS Access is using strongly typed datasets and Linq-to-DataSet http://msdn.microsoft.com/en-us/library/bb386977.aspx
Or, considering going with SQL Express instead (it's free) http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/express.aspx
There is a MS Access EF 6.1 provider here
https://jetentityframeworkprovider.codeplex.com/
EDIT
Now the EF provider for Access is hosted on GitHub
https://github.com/bubibubi/JetEntityFrameworkProvider

EF Code first database generation

I have a MVC3 website I have been playing with, and the database is quite well populated. I need to change the underlying models, but of course, the standard approach would drop all data. Having the CREATE SQL (so I can ensure all fields/relationships are in line with the models), and the calculated hash (so EF thinks the models match the database) would allow me to manually make changes to the database.
Is there a way to interrogate a DataContext (or some other object) to:
1. Get the SQL it would use to generate the dataschema; and
2. Get the ERM Metadata hash
I have considered some other migration options, but just want to explore this avenue.
Edit: This is EF4.1, and is running against SQL 2008 R2 if that is of any relevance.
Thanks
Andrew
You can do it with EF Migrations. You'd need to upgrade your project to EF 4.3. You'd use the "Update-Database -Script" ps command.
Here is a link to the ASP.Net team blog on it: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
Pluralsight also has a course on it. They also have a free trial. http://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName=efmigrations

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.