简体   繁体   中英

DbContext onModelCreating filter data

I have in my ASP.NET Core 2.0 App in the DBContext class following Code. 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? 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. How do I get the data now in my controller class?

ToTable simply maps the object to the table. Until you request data with a linq statement its not doing anything. When you call ToList on the where clause then you EF will reach out to the database and query returning only the desired results.

All that is is doing is saying that MyTable maps to MyTable in sql. Nothing more.

If you want to specifically filter the entire result set permanently you can do so with Eneity.HasQueryFilter(y => y.ID == 4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM