C# 在EntityFramework CodeFirst中过滤DbSet
这是应用程序用户:C# 在EntityFramework CodeFirst中过滤DbSet,c#,asp.net-mvc,entity-framework,dbcontext,C#,Asp.net Mvc,Entity Framework,Dbcontext,这是应用程序用户: public class ApplicationUser : IdentityUser<long> { public string Firstname { get; set; } public string Lastname { get; set; } public UserTypes Type { get; set; } public string FullName { get { return $"{Firstname ?? "
public class ApplicationUser : IdentityUser<long>
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public UserTypes Type { get; set; }
public string FullName { get { return $"{Firstname ?? ""} {Lastname ?? ""}".Trim(); } }
}
public virtual DbSet<Provider> Providers{get;set;}
public virtual DbSet<Supporter> Supporters{get;set;}
现在在ApplicationDbContext
中,我希望在ApplicationUser旁边有这些DbSet
s:
public class ApplicationUser : IdentityUser<long>
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public UserTypes Type { get; set; }
public string FullName { get { return $"{Firstname ?? ""} {Lastname ?? ""}".Trim(); } }
}
public virtual DbSet<Provider> Providers{get;set;}
public virtual DbSet<Supporter> Supporters{get;set;}
公共虚拟数据库集提供程序{get;set;}
公共虚拟数据库集{get;set;}
哪个DbSet
应该返回ApplicationUsers
,其用户类型等于2(例如)公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
公共应用程序上下文()
:base()
{
}
公共虚拟IEnumerable提供程序
{
得到
{
返回(IEnumerable)Users.Where(z=>z.Type==UserTypes.Provider).AsEnumerable();
}
}
}
UserTypes的属性是什么?@H.Herzl这是一个枚举{Normal=1,Provider=2,Supporter=3}
您尝试过这个吗?var query=dbContext.Providers.Where(item=>item.Type==UserTypes.Provider.ToList();这是我一直做的事。我想让它成为一个具有自己独特属性(类似于sql中的视图)的虚拟数据库集。我不明白,您是否尝试过该代码但不起作用?