views:

23

answers:

1

Hi,

I have added a column 'IsDeleted' to every entity in my Entity Framework 4.0 model and implemented an Interface for it. How can i accomplish that the entities with 'IsDeleted' set to 'true' are ignored by all Objectsets and Navigationproperties in my model? Filtering the result using LinQ does not work i think, because the result can not be transformed back into an ObjectSet.

Can anybody help me please?

BTW: My template generates ObjectSets in the context-class like this:

Private _Persons As ObjectSet(Of Person)
Public ReadOnly Property Persons() As ObjectSet(Of Person)
    Get
        If (_Persons Is Nothing) Then
            _Persons = MyBase.CreateObjectSet(Of Person)("Persons")
        End If
        Return _Persons
    End Get
End Property

and navigation properties for the entities like this one:

<XmlIgnoreAttribute()>
<SoapIgnoreAttribute()>
<DataMemberAttribute()>
<EdmRelationshipNavigationPropertyAttribute("Model", "Map_Persons_Organisations", "Persons")>
 Public Property Persons() As EntityCollection(Of Person)
    Get
        Return CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedCollection(Of Person)("Model.Map_Persons_Organisations", "Persons")
    End Get
    Set
        If (Not value Is Nothing)
            CType(Me, IEntityWithRelationships).RelationshipManager.InitializeRelatedCollection(Of Person)("Model.Map_Persons_Organisations", "Persons", value)
        End If
    End Set
End Property
A: 

What is the purpose of your IsDeleted flag? Objects marked for deletion have their status updated by the ObjectStateManager. You can determine if an object has been marked for deletion before SaveChanges() has been called by querying the ObjectStateManager.

If you want to exclude deleted entities from your queries, call SaveChanges() marking them for deletion.. You can also use AcceptChanges() to remove entities from the collection without committing the deletion to the database.

Dave Swersky
The purpose is that I don't want to lose the deleted data. It should only not (NEVER) be visible on the GUI. I don't think I can do this with AcceptChanges(). Adding 'WHERE IsDeleted = false' or something to every query is not an option either, because it would be forgotten once in a while.
BrezelBob
It sounds like you are trying to implement an "Archived" flag that indicates that an Entity has been "deactivated" and should not be accessible. That's an application-specific requirement outside EF's scope. If you don't want to have to filter all your queries on a flag like that, you should consider an Archive method that *copies* "deactivated" entities to an archive table.
Dave Swersky