Ignore records marked as deleted in navigation properties in EF 4.0
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 P开发者_JAVA百科roperty
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
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.
精彩评论