I’m currently study for exam 70-515 TS: Accessing Data with Microsoft .NET Framework 4 and I have come across some really helpful features that Entity Framework provides.
One feature that I can see myself constantly using is conditional table mappings. We always like to keep a history of inactive records by setting a flag in the database. My applications never needs this data, so I am always adding where clauses to filter inactive records out. With conditional mappings, you can basically hide these fields from your queries. This way, I never have to write a statement in my query to filter out the inactive records!
How to Implement Conditional Mapping
First, you will need to delete the field you would like to have the conditional mapping set on. For my entity table, I need to remove the active field. Just simply right click and delete the property.
If you don’t do this you get an ugly error like this… “The value specified for the condition is not compatible with the type of the member.” Errors are no fun, so lets avoid that!
Next, right click on the entity table, and select “Table Mappings”.
The field we are concerned with is the <Add a Condition> field. Select the field you want to conditional map. For me, it’s the active field.
Now, we need to set the Operator and Value/Property fields. For this purpose, we are leaving it as the default = sign operator and set the field to 1 to only ever query for active records. You do however, have another choice of Is which will allow you to set the property to “Null” or “Not Null”.