In my application the data in the database must never be deleted.
Entities have a property, Deleted, which can be set to true or false.
Entities which are virtually deleted can not go out from the database, they must not exist from the application perspective.
I am managing this by creating GetAll- methods (for example GetAllUsers) in my data access layer. The methods return only entities which are flaged as NOT deleted (!Deleted), and all other methods (for example GetUserById) retrieved data with these methods.
See the example below...
public IEnumerable<User> GetAllUsers()
{
return _dataContext.Users.Where(element => !element.Deleted);
}
public User GetUserById(string userName)
{
return GetAllUsers().FirstOrDefault(elt => elt.UserName.Equals(userName));
}
This architecture is very good from the perpsctive of reliability because I'm sure that I always extract NOT deleted entities, but I'm afraid that this is not efficient.
My question is very simple: is my application extracting all data from the User table (select * from User
) each time a specific User is requested, or is Entity Framework smart enough to understand what I want and translates to a SQL query of something like: select * from User where userName = @userName
?
If I'm extracting all data from the database each time I need a single user, then how can I change my code so that it creates the correct query? I would like to mantain the centralization, so that I don't have to specify !Deleted in each LINQ query.