Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a service which points to a DBContext of a database.This service runs daily and fetches few records and apply business rules.

I have 2 other similar databases with same schema.

Now i need to run the same service on these two databases also.

Can any one tell me the best way to work out with this requirement.

Thanks, Hari
Posted
Comments
Sergey Alexandrovich Kryukov 14-Aug-14 8:10am    
It does not matter how many databases are used. What prevents you from doing that?
—SA

1 solution

Just add a DbContext for each database that you're trying to access. EF should enforce a new connection string for each context.

C#
public class MyContext : DbContext
{
    // Member sets
}

public class FirstNewContext : DbContext
{
    // Member sets
}

public class SecondNewContext : DbContext
{
    // Member sets
}


From there, you should be able to enumerate those and iterate them for your daily check. If you're using POCOs you can even recycle those in the new classes.

C#
public static class DailyTask
{
    public static void CheckDataBases()
    {
        var contexts = new List<dbcontext> 
        { 
            new MyContext(), 
            new FirstNewContext(), 
            new secondNewContext()
        };

        foreach(var context in contexts)
        {
            DoWhateverDailyTaskYouWereDoing(context);
        }

}
</dbcontext>
 
Share this answer
 
Comments
Member 9600759 14-Aug-14 9:28am    
thank u for give solution to me
Member 9600759 14-Aug-14 10:35am    
Hi,

If the service timer is set to 30 secs i.e for every 30 secs the service restarts.

I have doubt that ,suppose to complete one loop in for each statement it requires more than 30 secs. then does the service runs for the other databases or not.

Once 30 secs completed , then again service restarts and works on first database.

I hope you have understood.

How can i balance service time interval and time took for looping and completing the tasks.
Nathan Minier 14-Aug-14 11:24am    
That is wholly dependent on how much data you're pulling and what you're doing with that data, network latency, etc. All told, it's entirely likely that 30 seconds is plenty of time for a local, relatively small operational pattern, but I would run a few tests to be certain.

That quick psudocode is largely based on your initial question, which stated that it was a daily operation(ie once a day).
Member 9600759 18-Aug-14 2:20am    
Hi,

If the service timer is set to 30 secs i.e for every 30 secs the service restarts.

I have doubt that ,suppose to complete one loop in for each statement it requires more than 30 secs. then does the service runs for the other databases or not.

Once 30 secs completed , then again service restarts and works on first database.

I hope you have understood.

How can i balance service time interval and time took for looping and completing the tasks.
Nathan Minier 18-Aug-14 8:29am    
Based on your needs, I think you'd be best off adding new threads to your service (so that it can do all the updates in parallel without actually looping) or adding a conditional into your current service execution that tracks completion of the loop. without knowing what you're using, it's hard to even write decent psuedocode for it. The second would definitely be the easier path.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900