I’m currently using conditional mappings in EF4 to filter out any records where their IsActive column is false. This works as intended, but I’m running into an issue when navigating relationships.
As an example, I have a one-to-many relationship where a Store can have many Inventory records. A Store can be marked IsActive as can the Inventory records that belong to it. Querying directly for records in those tables works as intended (only active records are returned), but I am also able to retrieve Inventory records where the associated store is not active. This allows the inactive Store to be accessed and gives me no way of detecting whether the store is active.
Prior to switching over to using conditional mapping, we were using queries similar to the following:
Inventories.Where(i => i.IsActive && i.Store.IsActive && i.Product.IsActive && i.Product.Id == productId);
I was hoping we could simplify that query to this:
Inventories.Where(i => i.Product.Id == productId);
This doesn’t work for me though, as I no longer have any way of knowing whether the Store or Product are active (as I can’t use conditional mapping on IsActive while still mapping that column).
Is there any way of replicating that query while taking advantage of conditional mapping in EF4? Am I forced to leave conditional mapping out of this and hope that all queries make sure to check all relevant IsActive fields?
While the other answer may work for some, we decided to map the EF EntitySet to a view instead. The view joins to appropriate tables and checks their appropriate
IsActivefields. The resulting portion of our EDMX looks similar to the following: