Possible to switch table at runtime using Entity Framework

entity-framework

I have code to switch between databases when using EF, but now also need to allow the user to choose a particular table. (The tables all use the same schema but may have different names because of the way the client updates datasets). Is there a way to do this? And if it's done can it be done without being broken by updates being made to the Entity data model?

Best Answer

This blog post goes into some detail: Changing Entity Framework model at runtime. It's apparently fairly difficult.

Out of the box there is no way, or at least not an easy one, to change table or schema names used by the Entity Framework at runtime...

On CodePlex however you can find an adapter for your Entity Framework model to change it at runtime. You can change the connection to the database, change table names or use different schemas...

So we’re all set to support multi tenancy? Not quite...

For a multi tenant application we need a different model for each of our customers so we need to make another change to the code as available in the CodePlex project. If you don’t mind taking the performance hit to rewrite the mapping every single time an object context is created you can just open the ConnectionAdapter class and change the static variables which hold the model information to instance variables...