简体   繁体   English

我可以在Entity Framework中合并来自多个数据库的对象吗?

[英]Can I union an object from multiple databases in Entity Framework?

I'm trying to figure out what the best way to tackle this would be. 我正在尝试找出解决此问题的最佳方法。 I have a Customer table in two different databases (not ideal I know, but it cannot be modified, I have to work with what I've been given). 我在两个不同的数据库中有一个Customer表(我知道不是很理想,但是不能修改,我必须使用给出的内容)。 The records are kept in synch and share several fields (eg CustomerId, CustomerName, etc). 记录保持同步并共享多个字段(例如,CustomerId,CustomerName等)。 But they also each have their own unique fields...so for example: 但是它们每个人都有各自独特的字段...因此,例如:

Database1 Customer Database1客户

  • CustomerId 顾客ID
  • CustomerName 顾客姓名
  • Field1 场1
  • Field2 场2

Database2 Customer Database2客户

  • CustomerId 顾客ID
  • CustomerName 顾客姓名
  • DifferentField1 DifferentField1
  • DifferntField2 DifferntField2

Each database has its own dbContext that I can independently pull each Customer object from. 每个数据库都有其自己的dbContext,我可以独立地从中提取每个Customer对象。 But what I would really like to have is a single unified Customer object that includes the union of all the fields in both dbs. 但是我真正想要拥有的是一个统一的Customer对象,其中包括两个数据库中所有字段的并集。

What would be the best way to accomplish this so that I can expose a single unified object? 什么是实现此目的以便暴露单个统一对象的最佳方法?

EDIT: I am using DbSet and specifying a mapping for each entity in my context object, like this: 编辑:我正在使用DbSet并为我的上下文对象中的每个实体指定一个映射,如下所示:

public DbSet<Customer> Customers { get; set; }

and then my mapping class has the typical mapping information: 然后我的映射类具有典型的映射信息:

this.ToTable("Customer");
this.HasKey(t => t.CustomerId);
this.Property(t => t.CustomerName);

etc. so I'm looking for a way to extend this logic with multiple tables/databases and be able to perform not just the queries but all the necessary CRUD operations 等。因此,我正在寻找一种方法,以使用多个表/数据库扩展此逻辑,并且不仅可以执行查询,还可以执行所有必要的CRUD操作

thanks 谢谢

All you need to do is to declare a class which will represent an unified object: 您需要做的就是声明一个代表统一对象的类:

public class UnifiedCustomer
{
    public int CustomerId {get; set;}
    public string Name {get; set;}
    public int Field1 {get; set;}
    public int Field2 {get; set;}
    public int DifferentField1 {get; set;}
    public int DifferentField2 {get; set;}
}

This type will be used to represent data from both databases: 此类型将用于表示来自两个数据库的数据:

var customers1 = dataContext1.Customers.Select(c => new UnifiedCustomer
{
    CustomerId = c.CustomerId,
    Name = c.CustomerName,
    Field1 = c.Field1,
    Field2 = c.Field2
});
var cusomers2 = dataContext2.Customers.Select(c => new UnifiedCustomer
{
    CustomerId = c.CustomerId,
    Name = c.CustomerName,
    DifferentField1 = c.DifferentField1,
    DifferentField2 = c.DifferentField2
});

Having both collections of the same type you can perform union: 具有两个相同类型的集合,您可以执行联合:

var all = customers1.Union(customers2);

What would be the best way to accomplish this so that I can expose a single unified object? 什么是实现此目的以便暴露单个统一对象的最佳方法?

You'll likely need to pull in the appropriate results from each context, then use LINQ to Objects to perform the "union" on the result sets in memory. 您可能需要从每个上下文中提取适当的结果,然后使用LINQ to Objects对内存中的结果集执行“联合”。

If two database are mapped to same class you can simple do: 如果将两个数据库映射到同一类,则可以执行以下操作:

        using (var cx1 = new customerEntities(firstDbConnectionString))
        {
            using (var cx2 = new customerEntities(secondDbConnectionString))
            {
                return cx1.tCustomer.ToList().AddRange(cx2.tCustomer.ToList());
            }
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM