Repository navigation
Description
In the age of cloud it becomes more common to shard across databases based on tenant, geography and scaling needs. EF should support querying across databases better.
It's not enough to change the connection string because one query might need to pull from multiple databases. Example: Join customer-specific data with master data.
I think it's really required that EF allows us to specify the database name per table reference. A strawman:
from o in db.Orders.WithDatabase("NewYork-Tenant1234") join c in db.Customers.WithDatabase("NewYork-master") on ... select new { o, c }This is supposed to be a scenario where the database name depends on a geographical sharding as well as on the tenant name. Both are dynamic and can change for every query. Connection strings cannot do this. Statically configured database names cannot do that either.
This query would translate to:
select * from [NewYork-Tenant1234].dbo.Orders o join [NewYork-master].dbo.Customers c on ...The same configuration mechanism would lend itself well to configuring hints such as
NOLOCKor the commonUPDLOCK, ROWLOCK, HOLDLOCKcombination. This is unrelated to this issue but I'm mentioning this to justify with theWithDatabasesyntax makes sense.A simple extension to this would be to allow completely dynamic table names.
WithTableName(database, schema, name)would map todatabase.schema.nameon the SQL side. This is useful for example when sharding by date with one table per day. SQL Server Standard Edition does not have partitioning.What is the team's stance on supporting such scenarios?
Activity
rowanmiller commentedon Jul 15, 2016
More notes from @GSPP
rowanmiller commentedon Jul 15, 2016
Split out from #4898
Antaris commentedon Sep 30, 2016
I tackled this a different way based on my requirements around building modular applications (not specifically multi-tenant).
My approach was to define what I call logical groupings, e.g. "Core", "Commerce", "Media", etc.
Several modules can belong to the same logical group, i.e. my Security module (Users, Roles, etc.) belongs in Core, as does my Settings module.
I defined an attribute which can be used at the context level:
This
LogicalGroupingattribute is used when determining which connection string to use. The LogicalGrouping is also used through my entity type configurations, so I can explicitly say that an entity belongs to a specific database. This enables me to support cross-database navigation properties. I.e., I could define a User object in my Core logical group, but then include it in a different context:This is handled through overriding the
SqlServerQuerySqlGenerator::VisitTablemethod:I know this is quite a specialised implementation, but I'd be interested to know what plumbing is changed to support the above request, should it affect my current implementation.
rigofunc commentedon Jan 20, 2017
@rowanmiller my team are hard to look for this feature, yes, we need this feature too.
Our scenarios is:
grahamehorner commentedon Mar 17, 2017
Just like to vote up this feature; we are looking at providing a multi-tenant restful API which switches database context / schema based on URI / JWT user claims; it would be great in to have some way of resolving / dynamically creating a context / connection based on some predicate filter, storing / pooling the resulting instance so it can be reused.
seriouz commentedon May 3, 2017
@rowanmiller Any news on this topic?
Edit: I just found this link, and it seems this is the solution for the problem: http://benfoster.io/blog/aspnet-core-multi-tenancy-data-isolation-with-entity-framework
seblucas commentedon Sep 28, 2017
I also like to vote for this feature (or at least better documentation on the subject).
I want to have one database per customer (to make perfectly sure no data can be crossed between customers)
I can load a specific connection string based on some information coming from a JWT token (basically what Ben Foster proposed), I'm going to make a POC about it but I'm confident it'll work. I can also use what's explained in #9616. That means having one DbContext that can be different every time.
Now with EF Core 2.0, we have DbContext pooling ... which is a very good news for performance and all but I have absolutely no idea of how it'll work when every Context created can be totally different from each other.
On the other hand I can create 30~40 different DBContext (DBContextCustomerA, DBContextCustomerB, ...) and having a Factory choosing between everyone of them ...... But it's against every principle I have :(
I guess there should be a proper documentation of what is feasible with EF Core and what's the recommanded way of handling many usecase.
stumpykilo commentedon Apr 20, 2018
Did the POC for what @seblucas suggested get created? Specifically the "load a specific connection string based on some information coming from a JWT token" part? Would be interested in how to do this.
pilucdes commentedon Oct 24, 2018
What's up with this ? This feature is important to us , we would use this to group some client together instead of using a 1 bucket database.. Our client need to be grouping
while maintaining their data integrity
ajcvickers commentedon Oct 24, 2018
@pilouk This issue is in the Backlog milestone. This means that it is not going to happen for the 3.0 release. We will re-assess the backlog following the 3.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
KLuuKer commentedon Sep 13, 2019
For us a way to specify\construct a different sql connection string would be enough.
Because of scaling issues we need to split our customers over separate databases AND physical database machines
Preferably WITH db context pooling support of course
santosh2812 commentedon Apr 2, 2020
Hi,
I am trying to implement Multi-Tenant -per Tenant separate Schema (Database single Instance).
https://gunnarpeipman.com/aspnet-core-database-per-tenant/ Taking help from this tutorial. Now I have one Situation That the Owner of the Application wants to have a dashboard which will show data from all the Tenant - Is this possible to connect with different DBContext and get data, the number of Tenant is not fixed it can increase at run time.
Apart from that -
Is this possible to create database at runtime - when the send metadata is added in DB - That time if we can have a button to initiate the the process.
Need some suggestion- which way should I go.
thanks
seriouz commentedon Apr 2, 2020
@pilouk My orga has this as a selling point: Database per customer
pilucdes commentedon Apr 2, 2020
I understand, there are many benefits to database per customer.
However, cross query on multiple context /database is not really performant.
Maybe it's possible to use the elastic search on top ?
seriouz commentedon Apr 2, 2020
In my case its easy because the customers are not connected and every customer has its own instance of the .net app. I don't know about @santosh2812
pilucdes commentedon Apr 2, 2020
So you don't need to cross query on all context right?
seriouz commentedon Apr 2, 2020
No we decided against this idea because of the problems named in the issue. But it would be great if we do not have to run the exact same app 80 times.
mrlife commentedon Jul 8, 2020
@ajcvickers Possible to provide an update or workaround for supporting a query that accesses 2 databases?
ajcvickers commentedon Jul 10, 2020
@mrlife I'm not aware of any workarounds that would allow a single EF context to access multiple databases in the same query.
roji commentedon Jul 10, 2020
It's possible to have a single LINQ query that performs a client-side set operation (Concat/Union) or some sort of join over data coming from two different databases (and therefore from two different DbContext instances). Not sure how helpful that is though.
mrlife commentedon Jul 14, 2020
@ajcvickers Thanks for letting me know. For implementing in EF Core source code, it must be more complicated than adding the name of the database onto beginning of a table reference, e.g. [db-name].[schema].[table-name]?
@roji Thanks, that is worth mentioning. The performance would be a consideration, but it could work for some situations.