Dynamically changing connections with the ADO.NET Entity Framework

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();

RunOneSetOfCode(data);

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?
troycompAsked:
Who is Participating?
 
Rouneh10Commented:
// 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);
0
 
Rouneh10Commented:
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();

   RunOneSetOfCode(Data);

   private void RunOneSetOfCode(ObjectContext Data)
   {
       // Do stuff here
   }

Open in new window

0
 
troycompAuthor Commented:
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
0
 
troycompAuthor Commented:
You are the man. Thank you so much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.