none
Extending EF General Querying Logic RRS feed

  • Question

  • Hi,

    I'd like to hear if there is any way in EF to override querying logic globally, for example:

    If I write _context.Employees, _context.Companies I'd like in both these queries to select entities that are not deleted because I want to have some global mechanism for deletion (marking as deleted) of all the entity types.

     

    Thank you


    Clarity VS Precision
    Wednesday, October 19, 2011 8:41 AM

All replies

  • Hi Giorgi,

    What I think you want to do, is create a Conditional Mapping.  This is where you specify a column (or columns) in your mapping details for the entity to have a "where" condition.  If you're using the designer, go to the EDMX and select the entity and right click and select the Table Mapping.  From here, you can go to the part at the top that says <add a condition> and you can select the column you want to filter on (for example an IsDeleted column) and then select an Operator "=" and specify a value and you could put "N".  So that would now cause the context to only return an ObjectSet of that entity that are not deleted.


    Tom Overton
    • Edited by Tom_Overton Wednesday, October 19, 2011 12:33 PM
    Wednesday, October 19, 2011 12:32 PM
  • Thank tom,

    there are two problems:

    1) I'm using EF 4.1 Code First

    2) I want to save all the "deleted" entities together in another table with two collumns - TableName, EntityId

    so that actually I'm not touching the entity itself but I'm saving the information about its deletion somewhere else.

    Hope this explains better what I'm trying to achieve.

     

    Regards


    Clarity VS Precision
    Wednesday, October 19, 2011 2:15 PM
  • Hi,

    Before SaveChanges we can drap down to ObjectStateManager to query the deleted entities by states:

    public IEnumerable<ObjectStateEntry> GetObjectStateEntries(
    	EntityState state
    )
    
    
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 25, 2011 7:07 AM
    Moderator
  • Hi Alan,

    thanks for the reply but honestly I don't understand what you mean.

     

    With respect


    Clarity VS Precision
    Tuesday, October 25, 2011 2:18 PM