Tie Entity Framework调用一个常见参数,例如UserId

时间:2022-10-05 14:49:52

We're creating an application with Entity Framework that deals with data specific to a single user. The database supports unlimited users so we do need to filter by a UserId. The SQL Server backend has a common column for all tables (INT UserId). With every query to the database we have to pass and evaluate a UserId param such as in LINQ, etc. Is there a good way that we can remove this code so that all queries automatically pass a UserId set at some higher level? For example, if I want to GetFoo() from the database for a UserId I can have the application call context.GetFoo() and not have to pass the UserId as a param? Maybe when creating the context for example we could pass the UserId at that level?

我们正在使用Entity Framework创建一个应用程序来处理特定于单个用户的数据。数据库支持无限用户,因此我们需要按UserId进行过滤。 SQL Server后端具有所有表的公共列(INT UserId)。对于数据库的每个查询,我们必须传递和评估UserId参数,例如LINQ等。有没有一种好的方法可以删除此代码,以便所有查询自动传递更高级别的UserId集?例如,如果我想从UserId的数据库中获取GetFoo(),我可以让应用程序调用context.GetFoo()而不必将UserId作为参数传递?也许在创建上下文时,我们可以在该级别传递UserId?

2 个解决方案

#1


0  

You can use this library: https://github.com/jcachat/EntityFramework.DynamicFilters

您可以使用此库:https://github.com/jcachat/EntityFramework.DynamicFilters

Just implement an interface w/ a userId on all entities you need to filter by user Id.

只需在需要按用户ID过滤的所有实体上实现具有userId的接口。

public interface IUserEntity
{
  int UserId {get; set;}
}

Pass in the userId to your DbContext, then in OnModelCreating:

将userId传递给DbContext,然后传入OnModelCreating:

modelBuilder.Filter("UserId", (IUserEntity u) => u.UserId, _userId);

#2


0  

You can use the following

您可以使用以下内容

in your DbContext, you can define a static property like this

在DbContext中,您可以定义这样的静态属性

public static int UserId;

and then you define the following interface

然后定义以下界面

public interface IUser
{
   int UserId {get; set;}
}

then you will decorate all the entities that have UserId by the above IUser interface

然后你将通过上面的IUser接口装饰所有具有UserId的实体

and inside your DbContext you can write the following:

在DbContext中,您可以编写以下内容:

public static IQueryable<T> WhereUser<T>(this DbSet<T> query, Expression<Func<T, bool>> predicate) where T : class,IUser
{
    return query.Where(predicate).Where(t => t.UserId == UserId); 
}

you can use the above code as follows

你可以使用上面的代码如下

using(var db = new YourDbContext())
{
    var details = db.YourDbSet.WhereUser(t=>t.IsActive); 
}

hope this will help you

希望对你有帮助

#1


0  

You can use this library: https://github.com/jcachat/EntityFramework.DynamicFilters

您可以使用此库:https://github.com/jcachat/EntityFramework.DynamicFilters

Just implement an interface w/ a userId on all entities you need to filter by user Id.

只需在需要按用户ID过滤的所有实体上实现具有userId的接口。

public interface IUserEntity
{
  int UserId {get; set;}
}

Pass in the userId to your DbContext, then in OnModelCreating:

将userId传递给DbContext,然后传入OnModelCreating:

modelBuilder.Filter("UserId", (IUserEntity u) => u.UserId, _userId);

#2


0  

You can use the following

您可以使用以下内容

in your DbContext, you can define a static property like this

在DbContext中,您可以定义这样的静态属性

public static int UserId;

and then you define the following interface

然后定义以下界面

public interface IUser
{
   int UserId {get; set;}
}

then you will decorate all the entities that have UserId by the above IUser interface

然后你将通过上面的IUser接口装饰所有具有UserId的实体

and inside your DbContext you can write the following:

在DbContext中,您可以编写以下内容:

public static IQueryable<T> WhereUser<T>(this DbSet<T> query, Expression<Func<T, bool>> predicate) where T : class,IUser
{
    return query.Where(predicate).Where(t => t.UserId == UserId); 
}

you can use the above code as follows

你可以使用上面的代码如下

using(var db = new YourDbContext())
{
    var details = db.YourDbSet.WhereUser(t=>t.IsActive); 
}

hope this will help you

希望对你有帮助