Dynamically changing connections with the ADO.NET Entity Framework

Posted on 2011-10-26
Last Modified: 2012-05-12
I have a .net 4 web application that has numerous ADO.NET entity framework models. These models are all the same sans the data thats in them. We will eventually combine all the databases into one, but thats way down the road. In the meantime i have multiple models.

The user will select a client from a dropdown list, and then the app will show data for that client. They can choose another client at anytime. Is there a way to dynamically change which model you want to pull dala from? Let me elaborate.

One of my models is called fnshipModel.edmx, another is esshipModel. I dont want to write code like this:

if (client == "fnship") {
var data = new fnshipEntities();
// do some work here

else if (client == "esship"){
var data = new esshipEntities();
// do the samework here just look at a different model

So forth and so on. I would like to do it like this:

SomeObjectThatTheEntityFrameworkUsesWhenItsBeingCreated data = null;

if (cleint == "fnship")
      data = new fnshipEntity();
else if (cleint == "esship")
     data = new esshipEntity();


private void RunOneSetOfCode(SomeObjectThatTheEntityFrameworkUsesWhenItsBeingCreated  data)
use data anyway i like and if i need to make a change, i change it in one place and not in multiple places

Is this possible?
Question by:troycomp
    LVL 3

    Expert Comment

    The Entities inherit from ObjectContext. Have you tried using that?

       ObjectContext Data;
       if (client == "fnship")
          Data = new fnshipEntities();
       else if (client == "esship")
          Data = new esshipEntities();
       private void RunOneSetOfCode(ObjectContext Data)
           // Do stuff here

    Open in new window


    Author Comment

    I tried that and that doesnt work. When i do this:

         var data = new fnshipEntities();
          foreach (var product in data.Products)
                            var x = product.ProductId;

    It works like a charm. But when I try it your way, and i thought this would work:
          ObjectContext data = null;
          data = new fnshipEntities();

        foreach (var product in data.Products) <-- compiler error cannot resolve symbol Products
            var x = product.ProductDescription;

    It doesnt see my tables or anything. Maybe the entity framework was written this way on purpose. But this seems weird to me. The entity framework is just a class like any other class and should behave the same
    LVL 3

    Accepted Solution

    // Create the ObjectContext.
    ObjectContext context =
        new fnhipEntities();
    ObjectSet<Product> query = context.CreateObjectSet<Product>();

    // Iterate through the collection of Products.
    foreach (Product result in query)
        Console.WriteLine("Product Name: {0}", result.Name);

    Author Comment

    You are the man. Thank you so much

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now