Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6134369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:20:48+00:00 2026-05-23T17:20:48+00:00

As part of a profiling tool, I have a custom ADO.NET stack that acts

  • 0

As part of a profiling tool, I have a custom ADO.NET stack that acts as a “decorator” around standard ADO.NET, so it doesn’t actually do any work – it just passes on the calls (but with logging, etc). Among other things, I have provided a DbProviderFactory from my custom connection that implements IServiceProvider and supplies a custom DbProviderServices.

This works great for most tools, including LINQ-to-SQL – however, Entity Framework is not happy.

For example – say I have:

MetadataWorkspace workspace = new MetadataWorkspace(
     new string[] { "res://*/" }, 
     new Assembly[] { Assembly.GetExecutingAssembly() });
using(var conn = /* my custom wrapped DbConnection */)
{
    var provider = DbProviderServices
          .GetProviderServices(conn); // returns my custom DbProviderServices
    var factory = DbProviderServices
          .GetProviderFactory(conn); // returns my custom DbProviderFactory
    ...

so far so good – the above two lines work; the correct (custom) provider info is returned.

Now we can add an EF model:

    using (var ec = new EntityConnection(workspace,conn))
    using (var model = new Entities(ec))
    {
        count = model.Users.Count(); // BOOM!
    }

fails with exception:

Unable to cast object of type ‘(my custom connection)’ to type ‘System.Data.SqlClient.SqlConnection’.

which is during assignment of a connection to a command; essentially, it has defaulted to the sql-server provider for the SSpace, and generated a naked SqlCommand. It is then trying to assign conn to the generated command, which can’t work (it will work correctly if all the decorators are in place, and a decorated DbCommand was used instead).

Now, the whole point of wrapping this on the fly means I don’t really want to have to change the EDMX to register a separate factory. I just want it to know about my lies, damned lies and decorators.

The following works, by hacking into the guts of SSpace (setting a private readonly field that I have no rights to know about or abuse):

    StoreItemCollection itemCollection =
        (StoreItemCollection)workspace.GetItemCollection(DataSpace.SSpace);
    itemCollection.GetType().GetField("_providerFactory",
        BindingFlags.NonPublic | BindingFlags.Instance)
        .SetValue(itemCollection, factory);

With this in place, the correct factory is used by SSpace. However, this is clearly nasty upon nasty.

So: am I missing a trick here? How can I intercept the EF provider with less drastic measures?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:20:49+00:00Added an answer on May 23, 2026 at 5:20 pm

    It should be possible to use the approach from the EFProviderWrappers. I know you mentioned you tried it but I just fought through this kind of thing myself with success.

    In your code sample above, you can’t pass a standard MetadataWorkspace to the EntityConnection constructor because the MetadataWorkspace will still point to the original DbProviderFactory (in your case SqlClient) in the SSDL section. I know you don’t want to be modifying your EDMX/SSDL directly (neither did I) but the EFProviderWrappers toolkit has some helper methods that might be of use to you. Especially helpful is the EntityConnectionWrapperUtils.cs class. It will take your original EDMX (it can even extract it from an embedded resource) and update the XML so it points to your custom DbProviderFactory. And it does this all at runtime so you don’t need to make any changes. You’ll need to come up with an Invariant name for your DbProviderFactory and register it – if you haven’t done that already.

    Then you can pass the custom MetadataWorkspace along with your custom DbConnection to the EntityConnection constructor and EF should then call to your factory when it needs to make a DbCommand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Part of a new product I have been assigned to work on involves server-side
Part of our java application needs to run javascript that is written by non-developers.
I'm writing a little JavaScript tool for myself that I plan on making available
I am trying the google performance tool for CPU time profiling. However, I had
My team and I are designing a diagnostic test tool as part of our
Part of my everyday work is maintaining and extending legacy VB6 applications. A common
Part of the install for an app I am responsible for, compiles some C
Part of the series of controls I am working on obviously involves me lumping
Part of the setup routine for the product I'm working on installs a database
Part of my latest webapp needs to write to file a fair amount as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.