Reputation: 436
i am creating a sort of data importer and I have to work from different databases/tables, depending on the source database I'm importing.
So I started creating a Model with EF for every type of database I have to work with.
Based on the application setting, I have to import data from a particular database.
So what I want to do is something like that:
var myDB; //the schema I need;
switch(DBType){
case DBTypes.Application1:
myDB = new APP1_Entities();
break;
case DBTypes.Application2:
myDB = new APP1_Entities();
break;
}
There is some way I can do this (or some kind of) with C# and EF? Thanks for everyone!
Upvotes: 0
Views: 430
Reputation: 4595
A good way to solve this would be through dependency injection. This way the dependency is inverted and you are not "newing up" the context in the method.
public void DoSomething(DbContext context)
{
myDb = context;
//other logic here
}
This gives control of the method to the caller. For more information there are plenty of resources all over the web. MSDN documentation is here: https://msdn.microsoft.com/en-us/library/hh323705(v=vs.100).aspx
Upvotes: 2