I have customer data stored in two different databases. The records are kept in synch and share several fields (e.g. CustomerId, CustomerName, etc). But they also each have their own unique fields…so for example:
Database1 Customer
CustomerId
CustomerName
Field1
Field2
Database2 Customer
CustomerId
CustomerName
DifferentField1
DifferntField2
Each database has its own dbContext that I can independently pull each Customer object from. But what I would really like to have is to use a single dbContext to pull in all the fields into unified Customer object that includes the union of all the fields in both dbs. Is this even possible or advisable?
The rest of my objects in my contexts are wired up automatically using DbSets and specifying a mapping for each entity, like this:
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);
this.Property(t => t.Field1);
etc.
So I’m wondering if there is a way to build on this logic with multiple tables/databases and be able to perform not just the selects but all the necessary CRUD operations. Is this even possible to do by overriding the default behavior or not? Or am I off base with how I am approaching the problem?
thanks
You should probably consider abstracting your DB contexts into a repository. So your repository is responsible for marshalling all the data from disparate tiers into a single object for you to use, and each DBContext is then only responsible for it’s specific tier.
There is not going to be getting around that there are two tiers unless you make some kind of linked server call from the DB itself and expose that value in one context (which I wouldnt recommend).