简体   繁体   English

DbContext onModelCreating 过滤数据

[英]DbContext onModelCreating filter data

I have in my ASP.NET Core 2.0 App in the DBContext class following Code.我在我的 ASP.NET Core 2.0 应用程序的 DBContext 类中遵循代码。 DbContext数据库上下文

public DbSet<MyTable> MyTable1 { get; set; }
...
protected override void OnModelCreating(ModelBuilder modelBuilder) {
  modelBuilder.Entity<MyTable>().ToTable("MyTable");

is it correct the .ToTable always gets the whole content of the table? .ToTable总是获取表格的全部内容是否正确? I need only certain data (filter).我只需要某些数据(过滤器)。 for example get projects from table Project which employees ID = 4. The employee ID I have but after successful login.例如从表 Project 中获取项目,其中员工 ID = 4。我拥有但成功登录后的员工 ID。 How do I get the data now in my controller class?我现在如何在我的控制器类中获取数据?

ToTable simply maps the object to the table. ToTable 只是将对象映射到表。 Until you request data with a linq statement its not doing anything.在您使用 linq 语句请求数据之前,它什么都不做。 When you call ToList on the where clause then you EF will reach out to the database and query returning only the desired results.当您在 where 子句上调用 ToList 时,EF 将访问数据库并查询仅返回所需结果。

All that is is doing is saying that MyTable maps to MyTable in sql.所做的只是说 MyTable 映射到 sql 中的 MyTable。 Nothing more.而已。

If you want to specifically filter the entire result set permanently you can do so with Eneity.HasQueryFilter(y => y.ID == 4);如果您想永久过滤整个结果集,您可以使用 Eneity.HasQueryFilter(y => y.ID == 4);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 DbContext 中无法访问创建的实体 OnModelCreating - Entity Created OnModelCreating is not accessible in DbContext EF Core 2.1中的动态数据种子迁移生成取决于DbContext bool-flag以及OnModelCreating()中的用法 - Dynamic data seeding migration-generation in EF Core 2.1 depending on DbContext bool-flag with usage in OnModelCreating() 在启用自动生成主键的情况下,如何在 DbContext 的“OnModelCreating”方法期间播种主数据? - How do I seed the master data during "OnModelCreating" method of DbContext in case of auto generated primary key enabled? 提取由IDatabaseInitializer中的DbContext.OnModelCreating创建的配置 - Extracting the configuration created by DbContext.OnModelCreating in IDatabaseInitializer Emit DbContext.OnModelCreating每个上下文创建 - Emit DbContext.OnModelCreating on every context creation 使用Scaffold-DbContext的部分EF上下文OnModelCreating - Partial EF Context OnModelCreating Using Scaffold-DbContext 如何在 EntityFramework Core 中使用部分类和部分 OnModelCreating 方法扩展 DbContext - How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core 在依赖注入中使用 Entity Framework Core DbContext 时不调用 OnModelCreating - OnModelCreating is not called when Entity Framework Core DbContext is used in Dependency Injection 实体框架7 DbContext OnModelCreating为ApplicationUser字段指定外键 - Entity Framework 7 DbContext OnModelCreating specify foreign key for ApplicationUser field ef core new dbcontext 只执行一次 OnModelCreating - ef core new dbcontext executes OnModelCreating only once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM