EntityCommandExecutionException with EF 6 in project net 4.0? - entity-framework

I have using entity framework 6 in a project that is net 4.0, and when I instaled the nuget package, the project adds the reference to EntityFramework.dll and EntityFrameWork.SqlServer.dll.
However, I want to capture the exception EntityCommandExecutionException, but there is not avaliable in neither of the both dlls.
I know that I can add a reference to System.Data.Entity, but I don't konw if this a good solution, because when I try to use the enum EntityState there are a conflict because the are one in the System.Data.Entity and other in EntityFramework.dll.
So the quiestion is if it is a good idea to use the System.Data.Entity library with EF 6. And if it is a bad idea, which is the equivalent exception of EntityCommandExecutionException in EF 6?
Thanks.

EF6 does not use System.Data.Entity.dll so adding a reference to this assembly won't help since EF6 will throw its own EntityCommandExecutionException. Actually adding a reference to an EF6 project will create even more confusion since the two are not designed to work together even though the type names tend to be called the same (EF6 was build by merging System.Data.Entity.dll and EntityFramework.dll code bases but is not binary compatible with EF5). In your app you need add
using System.Data.Entity.Core;
since this is where the EntityCommandExecutionException type lives in EF6.

Related

AutoMapper EF6 ProjectToList<T> "Method not found"

I'm using the Automapper (v10) and Automapper EF6 (v2) library for the first time (I've used AutoMapper successfully before) with EF6.4 and I'm getting a weird issue with the following line of code
using (DashboardEntities db = new DashboardEntities())
return db.Dashboards.ProjectToList<DTO.Dashboard>();
Really just want to get all entities from the DB, but mapped to DTO objects so I can keep the UI and Data projects distinct and not have an EF6 reference in my UI project.
The above snippet builds but throws a runtime exception:
System.MissingMethodException: 'Method not found: System.Linq.IQueryable`1<System.__Canon>
AutoMapper.QueryableExtensions.Extensions.ProjectTo(System.Linq.IQueryable,
System.Linq.Expressions.Expression`1<System.Func`2<System.__Canon,System.Object>>[])
The documentation gives an example using an Async version of the above method, but in my scenario, I don't want this.
I don't get why I'm getting this MissingMethodException at runtime
I could use the basic Automapper library and add some lines of code, but that defeats the purpose of the AutoMapper EF6 plugin and also reminds of this blog post I saw a while back
Thanks in advance for any insights
In the ProjectToList method, try to pass in the IConfigurationProvider. For example:
db.Dashboards.ProjectToList<DTO.Dashboard>(Mapper.ConfigurationProvider);

BeginTransaction with IsolationLevel in EF Core

I'm trying to rewrite old library to use EntityFramework Core and I can't figure out how to begin transaction with specific isolation level.
Previously I was able to do something like this:
DbContext.Database.BeginTransaction(IsolationLevel.Snapshot);
What is alternative implementation in the EntityFramework Core?
The EF Core code is exactly the same.
DbContext.Database.BeginTransaction(IsolationLevel.Snapshot);
The only difference is that in EF Core the method with isolation level (as many others) is an extension method, defined in RelationalDatabaseFacadeExtensions class, and importantly, located in Microsoft.EntityFrameworkCore.Relational assembly.
So if you have using Microsoft.EntityFrameworkCore; and don't see it, add reference to the Microsoft.EntityFrameworkCore.Relational.dll assembly / package.
In addition to Ivan Stoev's answer, it is also important to use System.Data.IsolationLevel and not System.Transactions.IsolationLevel when calling BeginTransaction method.

EF6.Utility.CS.ttinclude: ArgumentNotNull does not exist

I just upgraded to EF6 from EF5 and I encounter this error in a custom T4 that connects to the DB using a DbContext from a different assembly.
File: EF6.Utility.CS.ttinclude
Compiling transformation: The name 'ArgumentNotNull' does not exist in the current context
What I've done is replacing EF.Utility.CS.ttinclude with EF6.Utility.CS.ttinclude, which solved another error about DbSet and DbContext not being found.
The T4 is very simple, like this one:
using(var context = new EntityContext)
return context.Entities.Where(x => 1==1);
Except for the EF include I only reference my own assemblies. The freshly-added Context is generating just fine (in another project).
What on earth could be wrong?
EF6.Utility.CS.ttinclude reference some static functions that are defined in the main template.tt so you need to have them in your template too.
Example:
Entity.tt defines ArgumentNotNull(T arg, string name) which is used in the EF6.Utility.CS.ttinclude (it's not the way we are used to have)
See the bottom of generated template from EF6 designer to see this missing functions
One way to solve this is moving almost all the code to a .cs file and then use that file in the T4 template. Then remove the EF ttinclude from the T4 template.
Works and is easy better praxis to follow.

What assemblies can I remove when removing Entity Framework from a .net project?

I have a cs webproject that used to use MS Entity Framwork (v4 or 5 I think) for data access. All data access code is now moved to LLBLGEN.
Which of the following references can I safely remove:
System
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Drawing
System.EnterpriseServices
System.Runtime.Serialization
System.Security
System.Transactions
System.Web
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Entity
System.Web.Extensions
System.Web.Services
System.Xml
System.Xml.Linq
It's hard to say for sure because we don't know what the rest of your project is using. At a minimum, you should be able to remove System.Data.Entity. The DataSetExtensions are still in the default list, but primarily apply to LINQ to Datasets which I don't think you'll be using so you should be safe to pull that. It's hard to say if you need the rest or not as we don't know your project.

InsertHistoryOperation throws InvalidOperationException while no Database is existing

i have big Trouble using the EntityFramework 5.0. After updating EF in our Project from EF4.2 to EF5.0. The Framework throws InvalidOperationExceptions in the TypeInitializer of InsertHistoryOperation during initializing of the database. (In the InnerException it says: "The List does not contain any element").
I tried using DropCreateAlwaysInitializer as well as DropWhenModelChanges. We don't need the Migration Feature (Sure, would be nice to have, but not recommend). Is there a way to disable the Migration Feature so the exception is not thrown. Or can somebody give me a hint what i did wrong?
The Model is very complex, so i think its not very useful to post it here.
best regards,
Chris
I tried with .Net4.0 and .Net4.5
Ok, i got it.
The Problem is that the EntityFramework since V5.0 is not able to work correctly if you merge it into (e.g. a Datalayer Assembly) or use it from a merged assembly. (ILMerge).