Initial conditional mapping of Entity Framework with code?

I have an object that I need to return only records where the given field value is greater than zero. I saw examples of conditional mapping in edmx, and it seems like what I need. However, my project is first in EF 4.1 code. Is there a way to do this using the first code approach?

0
source share
1 answer

I don’t think there is a built-in method to achieve this, however you can open the property in your DbContext where you apply filtering, initially it will be read-only, but I see no reason why you should not create your own DbSet Implementation. reflecting a return to another DbSet (ProxyDbSet)

Example using Readonly:

class MyDbContext : DbContext { public IDbSet<User> Users { get; set; } public IQueryable<User> Admins { get { return from user in users where user.Role == "admin" select user; } } } 
+5
source

Source: https://habr.com/ru/post/905861/


All Articles