Repository navigation
Closed as duplicate
Description
Issue based on #4019 and PR #4672
It nice to have a feature to access multiple database from same model context.
This feature pretty massive. We need to modify following areas:
- Add database property to relational type annotations and follow this property from explicit mapping to default query sql generator (to create proper table alias including database name)
- Enhance data model with new field for catalogue name
- Implement sql generator and model validator for code first migrations from model with multiple databases (it means we should modify all generators and migration operators)
- Implement additional annotations to support attribute database generation, not only explicit
There are still some open design questions that needs to be resolved:
- There is no database/catalog propery in TableAttribute
So we could not implement data annotation mapping to different database. There is a possible solution by introducing additional attribute for this need (like DatabaseAttribute) with single proprte Name. In this case usage could be fairly simple
[Database("Pink")]
[Table("Unicorns", Schema="Fluffy")]
class DifferentDatabaseItem{ }
Also there is should be modelBuilder interface to setup database catalogue manually. Something like this:
modelBuilder.Entity<MappingQueryTestBase.DifferentDatabaseItem>(e =>
{
e.Metadata.Relational().TableName = "Unicorns";
e.Metadata.Relational().Schema = "Fluffy";
e.Metadata.Relational().Database = "Pink";
});
For such models we should generate additional catalogue prefix for FROM and JOIN statements.
- There is issue with navigation property building. We could easily generate proper join statement cross catalogues, but there is no proper way to add foreign key for such properties.
There is couple possible solutions:
- Forbid adding navigation properties between databases. This aproach is not really healthy. If we implement this - there is no particullar reason to keep multiple catalogues in single model context.
- Allow adding navigation properties, but enhance migration to prevent generating foreighn key operations for this properties. In this case we could work with our entities without any penalties, but from data storage perspective we will not have any "real" foreighn keys between them.
- There is open question regards to should this be a relational concept, or specific to SQL Server. As far as i know this behaivior supported in MySql and Oracle (if placed on same server). Not sure for postgre SQL and sql lite.
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]ModelBuilder: Allow specifying multiple data datalogues in context[/-][+]ModelBuilder: Allow specifying multiple data сatalogues in context[/+]rowanmiller commentedon Mar 25, 2016
@anpete will help with guidance on this one
divega commentedon Jun 7, 2016
From @GSPP' comment in #5626:
[-]ModelBuilder: Allow specifying multiple data сatalogues in context[/-][+]Support for multiple databases in the same model[/+]laskoviymishka commentedon Jun 7, 2016
This is very usefull. Small question about linq API. Will it looks like this?
If so this information probably should be added as extension to
DbSet. All these logic related only for relation concept. So we need to reorgonize currentDbSetimplementation toDbSetandRelationalDbSet(which extend baseDbSetwith relational concepts).For the configuration hint (nolock etc) it probably should be separated in different issue.
And since this really hard coupling with MS SQL should be implemented as part of
Microsoft.EntityFrameworkCore.SqlServer, so it require new implementation ofRelationalDbSetlikeSqlServerDbSetwhich could contain any tweek you like (and maybe some api for extension).Any thoughts on this?
GSPP commentedon Jun 7, 2016
A few more points:
db.Orders.WithDatabase("Tenant1").Concat(db.Orders.WithDatabase("Tenant2")).Concat(db.Orders.WithDatabase("Tenant3"))...; this expression would, of course, be built through a loop and not hard-coded like that).from o in db.Orders.WithDatabase("Tenant") select WithDatabase(o.Customer, "Master"). Semantically, aWithDatabasewrapping call would be interpreted as returning the first argument unmodified but with location meta-data attached. This function call would only be valid inside of an expression tree. The same wrapping could be done for sequence navigation properties. If extension syntax is wanted this requires an extension method onSystem.Objectalas. This smells, I admit. So it could be a static methodQueryHelpers.WithDatabase.Since there are so many different use cases there should be a hierarchy of location configuation:
WithDatabaseinside of the query takes precedence over everything.myQuery.WithDefaultTableLocation(db, schema).ToList()).For each of these levels I can present you a real-world use case that I have had... If that hierarchy is properly document I don't think it's hard to understand or hard to work with.
Is there not an issue tracking table hints such as NOLOCK? There should be. It's important. Surely, the team is tracking it internally.
laskoviymishka commentedon Jun 7, 2016
@GSPP the use case is pretty clear here.
But question about API still open. Static
QueryHelpercan solve issue for sure.Migrations also pretty intresting question. We should determine where table locate and could we define a REAL foreighn key or just virtual one.
tillig commentedon Jun 17, 2016
I'm not sure if it falls into this category (if not, I'll open a new issue - let me know), but the ability for a single database to have a different table per tenant for the same entity would be good, too. Using the example above, it'd be like
Master/shared table for some things, tenant-specific table for others.
laskoviymishka commentedon Jun 17, 2016
You mean something like have different order table per tenant?
tillig commentedon Jun 17, 2016
@laskoviymishka Exactly. Rather than partition by database as noted in the earlier example, partition by table in a single database. (I'd like EF to be able to support that. I have a situation like this right now.)
7 remaining items
GSPP commentedon Jul 6, 2016
@rowanmiller this ticket goes far beyond the other ticket. The other ticket is about statically specifying a database name. This ticket includes dynamic options. If you want to deduplicate I suggest closing the other way around.
rowanmiller commentedon Jul 7, 2016
@GSPP fair point. The initial description does line up with #4019 but I agree the discussion in the comments talks about a lot more. Remarking for triage so that we can discuss this scenario.
GSPP commentedon Jul 7, 2016
Good call! I think this is important.
rowanmiller commentedon Jul 15, 2016
Given that this issue ended up being about a couple of features, we decided to track them separately:
Neither of these are things we are planning to tackle right away, so they are both on the backlog for the moment.
narendrasinghrathore commentedon May 4, 2017
@rowanmiller
Any update. How can one use one entity model to connect multiple database provider like mssql, mysql,oracle ,mongodb etc . Found this but not helpful https://www.codeproject.com/Articles/82017/Preparing-an-Entity-Framework-model-for-multi-prov https://www.codeproject.com/Tips/1062375/Entity-Framework-Multi-DB-Support
Please can you help on this.
ajcvickers commentedon May 8, 2017
@narendrasinghrathore This issue is about accessing multiple databases of the same kind--for example, multiple SQL Server databases. It's not about different types of databases. You seem to be wanting to access different types of database using the same model. There are several dimensions to this question, so it's hard to give an answer here. I would suggest either opening a new issue explaining clearly what it is you need to do, or perhaps doing the same thing but in a Stack Overflow question--you'll get more knowledgeable eyes on it on Stack Overflow.
Finally, keep in mind this GitHub repo is for EF Core. The articles you referenced are for earlier versions of EF, for which the best place to log issues is https://github.com/aspnet/EntityFramework6
narendrasinghrathore commentedon May 9, 2017
@ajcvickers Thanks,
https://github.com/aspnet/EntityFramework6/issues/269