Showing posts with label linq-to-sql. Show all posts
Showing posts with label linq-to-sql. Show all posts

Monday, December 21, 2015

Fix: “The default DbConfiguration instance was used by the Entity Framework before the '...' type was discovered” in LINQPad

For a while now, I’ve been using the excellent LINQPad to quickly run .Net Entity Framework queries using the actual object model and database mappings from one of my projects, via a DbContext connection pointed to my project’s compiled .dll file and web.config file set up in LINQPad.

Recently, after this having been working fine for a while, I started getting an error when trying to execute any queries using this DbContext connection in LINQPad:

The default DbConfiguration instance was used by the Entity Framework before the '...' type was discovered.

The "..." in that error message was actually the name of a new class that had been added to my .Net project, which derived from the DbConfiguration class.

The fix was, in my project’s web.config file, to add to the entityFramework element a codeConfigurationType attribute pointing to the new class and its package.  So, whereas before my web.config file had:

<entityFramework>
    ...
</entityFramework>

I changed it to:

<entityFramework codeConfigurationType="MyProject.MyPackage.MyCustomDBConfiguration, MyProject.MyPackage">
    ...
</entityFramework>

(Where "MyProject", "MyPackage", and "MyCustomDBConfiguration" all obviously are replaced by the actual project, package, and class names from my actual project.)

With that change in place, I was able to successfully execute queries using my project’s EF model once again.

Update 2016-06-06: A second possible cause of this same issue is if the path to your application config file (or web.config file) in LinqPad's Connection Properties dialog is incorrect.

I had this issue occur just now after moving my project to a new location on disk. I'd corrected the "Path to Custom Assembly" value in the Connection Properties dialog, but I initially neglected to make the corresponding filesystem path change to the the "Path to application config" value.

Tuesday, June 09, 2015

System.Data.Entity.Core.EntityCommandExecutionException

Problem: A particular LINQ-to-SQL query selecting fields from a SQL Server view in a C# program, which ran fine in my local dev environment, produced an exception when run in the staging environment:

Exception Message: An error occurred while executing the command definition. See the inner exception for details. 

Exception Trace: System.Data.Entity.Core.EntityCommandExecutionException 
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) 
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.b__5() 
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) 
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
at System.Data.Entity.Core.Objects.ObjectQuery`1..GetEnumerator>b__0() 
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() 
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
at [My code ...] 

Resolution: I had neglected to deploy to the staging environment (where the query was failing) a recently-created Entity Framework migration, which added a new output field to the SQL Server view being queried.  The query was failing because it was trying to select a field which didn’t actually exist on the target view.

Upon closer examination, when I reproduced the error and inspected the inner exception (which wasn’t being automatically written to the application’s error log), it was indeed of type SqlException and had the helpful message “Invalid column name ‘[my column name]’”.

So, things to try next time I see this exception:

  • Examine the inner exception (as suggested by the outer exception’s error message).
  • Make sure all Entity Framework migrations have been deployed to the target environment (if EF is in use).
  • Check and see whether the query is trying to select a field that doesn’t exist.