不同数据库上的单一窗口服务 [英] single window service on different databases

查看:96
本文介绍了不同数据库上的单一窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个指向数据库的DBContext的服务。该服务每天运行并获取少量记录并应用业务规则。



我有2个其他类似的具有相同模式的数据库。



现在我还需要在这两个数据库上运行相同的服务。



任何人都可以告诉我解决这个问题的最佳方法。



谢谢,Hari

解决方案

< blockquote>只需为您尝试访问的每个数据库添加DbContext。 EF应为每个上下文强制执行新的连接字符串。



  public   class  MyContext:DbContext 
{
// 成员集
}

public class FirstNewContext:DbContext
{
// 会员集
}

public class SecondNewContext:DbContext
{
// 会员集
}





从那里,您应该能够枚举这些并迭代它们进行日常检查。如果您正在使用POCO,您甚至可以在新课程中回收这些。



  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 >


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

解决方案

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

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.

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>


这篇关于不同数据库上的单一窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆