简体   繁体   English

如何处理EF数据库优先上下文列表并正确转换它们?

[英]How to deal with a list of EF database first contexts and cast them correctly?

I have a list of entity framework's 6.0 database first contexts with different names. 我有一个使用不同名称的实体框架的6.0数据库优先上下文列表。 They all contain a table named "bill". 它们都包含一个名为“ bill”的表。 I need to check in every database's bill table, and add informations into a single new database depending on a condition. 我需要检入每个数据库的账单表,并根据情况将信息添加到单个新数据库中。 Example : 范例:

  1. Company1_Entities
  2. Company2_Entities
  3. Company3_Entities

These 3 databases contain the bill table. 这3个数据库包含帐单表。 I need to store these bill conditionnaly into : 我需要有条件地将这些帐单存储到:

All_Bills_Entities

As the number of company will grow, I need to make this dynamic. 随着公司数量的增长,我需要使其充满活力。 I was thinking of something like this : 我在想这样的事情:

Dictionary<string, DbContext> lstDB = new Dictionary<string, DbContext>();

// I'm supposed to retrieve these db names from a table, but now i'm just testing

lstDB.Add("00439837", new DbContext("Company1_Entities"));
lstDB.Add("00439832", new DbContext("Company2_Entities"));
lstDB.Add("00439839", new DbContext("Company3_Entities"));

using (All_Bills_Entities main_db = new All_Bills_Entities())
{
    foreach(var dataBaseInfo in lstDB)
    {
        DbContext currentDB = dataBaseInfo.Value;

        foreach (var record in currentDB.bill.ToList()) // this does not compile, there is no bill table found here
        {
           if(record.merchant == dataBaseInfo.Key /* && other Conditions */)
           {
                main_db.bill.Add(record)
           }
        }
    }
}

The easiest solution is to make each context implement the same interface, for example: 最简单的解决方案是使每个上下文实现相同的接口,例如:

public interface IBillContext
{
    DbSet<Bill> bill { get; } 
}

Now make your contexts implement it: 现在,使您的上下文实现它:

public class Company1_Entities : IBillContext
{
    public DbSet<Bill> bill { get; set; }

    //etc...
}

And finally, change your list to use the interface: 最后,更改列表以使用界面:

Dictionary<string, IBillContext> lstDB = new Dictionary<string, IBillContext>();

Alternatively, if you know the type you can make use of the Set<T> property. 另外,如果您知道类型,则可以使用Set<T>属性。 For example assuming the entity type is Bill : 例如,假设实体类型为Bill

foreach (var record in currentDB.Set<Bill>().ToList())
{
   if(record.merchant == dataBaseInfo.Key /* && other Conditions */)
   {
        main_db.bill.Add(record)
   }
}

Of course this would fail at runtime if you added an entity to the dictionary that didn't have this DbSet. 当然,如果您将没有此DbSet的实体添加到字典中,则这将在运行时失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "如何首先在 EF 代码中为一个数据库处理在多个数据库上下文中使用的一个类?" - How to handle one class used in multiple db contexts for one database in EF code first? 如何处理 EF Core 数据库迁移失败? - How to deal with EF Core database migration failures? EF初始化程序,具有一个数据库的多个上下文 - EF Initializer with multiple contexts for one database 使用EF的Database First中的连接字符串无法正常工作 - Connection string in Database First using EF doesn't work correctly 如何使用EF Core在开发期间处理数据库更改? - How to deal with database changes during development with EF Core? 如何首先使用EF数据库检索数据 - How to retrieve data using EF Database First 如何将EF代码优先数据库发布到Azure - How to publish EF code first database to azure 第一次调用后,对不同数据库上下文的 EF 查询似乎会缓存 - EF queries to different DB contexts seem to cache after the first call 使用EF Code First的DDD - 如何将它们组合在一起? - DDD with EF Code First - how to put them together? 如何使用EF(DB优先)正确实现DI? - How to correctly implement DI using EF (DB first)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM