[英]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.