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

  • Home
  • SEARCH
  • 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 6344845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:43:12+00:00 2026-05-24T20:43:12+00:00

I’m using EF with 2 databases – with SQL CE and SQL Server. Is

  • 0

I’m using EF with 2 databases – with SQL CE and SQL Server.

Is there a way to know which connection type is used at runtime? I mean, if I have only ObjectContext in some place (already initialized with some connection string), can I get the database type from it (is it Compact or SQL Server at the moment)?

Thanks

  • 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-24T20:43:13+00:00Added an answer on May 24, 2026 at 8:43 pm

    You can check the Connection Property, which should return an EntityConnection; from there you must check its StoreConnection which will be the “real” database connection.

    From there, you can either check the ConnectionString, which will tell you the provider, or simply check the type of the provider connection itself with is or GetType. If it’s SQL Server it will be a SqlConnection, and if it’s SQL CE it will be a SqlCeConnection.

    This looks like an ugly hack because it is; if you’re looking for a way to do this without an ugly hack, don’t bother – the ObjectContext is explicitly designed not to leak any information about the connection unless you know exactly what to ask for. By contrast, here’s all the hoops you would have to jump through to check it via the app config:

    static string GetProviderName(ObjectContext context)
    {
        string entityConnectionString = GetEntityConnectionString(context);
        return !string.IsNullOrEmpty(entityConnectionString) ?
            GetProviderConnectionString(entityConnectionString) : null;
    }
    
    static string GetEntityConnectionString(ObjectContext context)
    {
        var match = Regex.Match(context.Connection.ConnectionString,
            @"name=(?<name>[^;]+)", RegexOptions.Compiled);
        string connectionStringName = match.Success ?
            match.Groups["name"].Value : null;
        return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    }
    
    static string GetProviderConnectionString(string entityConnectionString)
    {
        var match = Regex.Match(entityConnectionString,
            @"provider=(?<provider>[^;]+)", RegexOptions.Compiled);
        return match.Success ? match.Groups["provider"].Value : null;
    }
    

    Once any solution starts to involve regexes, I tend to look for a straighter path, and in this case, that’s the type casting you say you’d prefer not to use. Pick your poison.

    Be careful how you use either of the above approaches. EF4 is designed around persistence ignorance and you should be trying to avoid any logic that’s specific to the connection type, because you really have no idea how it will be configured (maybe tomorrow it will be an Oracle connection). I believe that the provider-specific code resides mainly in the QueryProvider.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.