Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

asp.net - Entity Framework for Multi-tenant architecture - filterings single table by tenant ID

We are looking for a way of automatically filtering all CRUD operations by a tenant ID in Entity Framework.

The ideas we thought of were:

  • Using table valued user defined functions
  • Using stored procedures (but we don't really want to, as we're using an ORM to avoid doing so)
  • Some how modifying the templates used to generate the SQL to add a where clause on each statement.
  • Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).

Any tips?

-thanks Alex.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using table valued user defined functions

Table valued function are only available in .NET 4.5 Beta (and not available in code first). Using them will still not help you because you will have to use the function in every LINQ query so it is the same as using where clause.

Using stored procedures (but we don't really want to, as we're using an ORM to avoid doing so)

It can be useful for some special complex queries but generally it is not what you want.

Some how modifying the templates used to generate the SQL to add a where clause on each statement.

Too complex and on completely different level of abstraction.

Some how modifying the templates used to generate the LINQ in the controllers (we may use MVC).

Close to ideal solution. You simply need to wrap access to your entity set into some code which will look like:

public class MultiTenantAccess<T> where T : IMultitenant
{
    private IDbSet<T> set;  

    ... 

    public IQueryable<T> GetQuery(int tenantID) 
    {
        return set.Where(e => e.TenantID == tenantID); 
    }
}

Sometimes this is core for something called Generic repository but it is really just a wrapper around EF set. You will always use GetQuery to query your data store instead of using DbSet directly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...