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.

6 comments:

  1. Exactly my issue. Thanks!

    ReplyDelete
  2. Jonathan, where can I find what to enter instead of: "MyProject", "MyPackage", and "MyCustomDBConfiguration"?
    I don't know what to replace it with..

    ReplyDelete
  3. Jonas, in my case, "MyCustomDbConfiguration" was the name of my class that inherits from DbConfiguration, and "MyProject.MyPackage" is the fully-specified namespace in which that class resides. Sorry it was unclear!

    ReplyDelete
  4. It works perfectly!! Thanks!

    ReplyDelete
  5. Thanks for the good explanation.

    ReplyDelete
  6. i updated my context file to

    //------------------------------------------------------------------------------
    //
    // This code was generated from a template.
    //
    // Manual changes to this file may cause unexpected behavior in your application.
    // Manual changes to this file will be overwritten if the code is regenerated.
    //
    //------------------------------------------------------------------------------

    namespace MrSales.MrSModels
    {
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;
    using System.Data.Common;


    public partial class mrsalesdbEntities : DbContext
    {
    public mrsalesdbEntities()
    : base("name=mrsalesdbEntities")
    {
    }


    public mrsalesdbEntities(DbConnection existingConnection, bool contextOwnsConnection)
    : base(existingConnection, contextOwnsConnection)
    {
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet account_items { get; set; }


    also i updated the app.config file to:




    but this error appear :
    The DbConfiguration type 'MrSales.MrSModels.mrsalesdbEntities, MrSales.MrSModels' specified in the application config file could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.'

    ReplyDelete

Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!

Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!