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?
- This is very strange. Why not use views instead of different table names?NoChance– NoChance2012-09-18 05:59:11 +00:00Commented Sep 18, 2012 at 5:59
- @Emmad Kareem I know. I've asked that question and the answer had something to do with the way other applications generate datasets for various geographic areas.Stonetip– Stonetip2012-09-18 14:13:34 +00:00Commented Sep 18, 2012 at 14:13
- Folks, please see note below in response to World Engineer's answer. I've decided we're just not going to pursue this avenue anyway.Stonetip– Stonetip2012-09-18 14:14:40 +00:00Commented Sep 18, 2012 at 14:14
1 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...
- 2Link only answers are bad answers. While it may be difficult is there any information you can include in the answer?ChrisF– ChrisF2012-09-18 12:58:54 +00:00Commented Sep 18, 2012 at 12:58
- @World Engineer Thanks for the link. I had come across that in my own research earlier. My thoughts were that it wasn't worth it. Incidentally, since some of our code base relies on ADO.NET and it supports async and await now (unlike EF), I've decided to just leave EF out of the project.Stonetip– Stonetip2012-09-18 14:11:39 +00:00Commented Sep 18, 2012 at 14:11
- @Stonetip: Not using EF for this scenario was the best and IMHO the only correct solution for this case. You should post it as an answer to your question.Ladislav Mrnka– Ladislav Mrnka2012-09-27 07:23:57 +00:00Commented Sep 27, 2012 at 7:23