拦截器:
Public Class SoftDeleteInterceptor
Implements Interception.IDbCommandTreeInterceptor
Public Const SoftDeleteColumnName = "Deprecated"
Public Sub TreeCreated(interceptionContext As Interception.DbCommandTreeInterceptionContext) Implements Interception.IDbCommandTreeInterceptor.TreeCreated
If interceptionContext.OriginalResult.DataSpace = Entity.Core.Metadata.Edm.DataSpace.SSpace Then
Dim queryCommand = TryCast(interceptionContext.Result, Entity.Core.Common.CommandTrees.DbQueryCommandTree)
If queryCommand IsNot Nothing AndAlso Not queryCommand.Query.ExpressionKind = DbExpressionKind.NewInstance Then
Dim expression = queryCommand.Query.Accept(New SoftDeleteQueryVisitor())
Dim query = New DbQueryCommandTree(queryCommand.MetadataWorkspace, queryCommand.DataSpace, expression)
interceptionContext.Result = query
End If
Dim deleteCommand = TryCast(interceptionContext.OriginalResult, DbDeleteCommandTree)
If deleteCommand IsNot Nothing Then
Dim type = Common.Reflection.GetEntityTypeFromString(deleteCommand.Target.VariableType.EdmType.Name, True)
If Not type Is Nothing Then
If type.GetInterfaces.Contains(GetType(ISoftDelete)) Then
Dim setClauses = New List(Of DbModificationClause)
Dim table = DirectCast(deleteCommand.Target.VariableType.EdmType, Entity.Core.Metadata.Edm.EntityType)
If table.Properties.Any(Function(p) p.Name = SoftDeleteColumnName) Then
setClauses.Add(DbExpressionBuilder.SetClause(DbExpressionBuilder.[Property](DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName), SoftDeleteColumnName), DbExpression.FromBoolean(True)))
End If
Dim update = New DbUpdateCommandTree(deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, setClauses.AsReadOnly(), Nothing)
interceptionContext.Result = update
End If
End If
End If
End If
End Sub
End Class
它为每个查询命令添加了一个ExpressionVisitor(称为SoftDeleteQueryVisitor)。
ExpressionVisitor:
Public Class SoftDeleteQueryVisitor
Inherits Entity.Core.Common.CommandTrees.DefaultExpressionVisitor
Public Const SoftDeleteColumnName = "Deprecated"
Public Overrides Function Visit(expression As DbScanExpression) As DbExpression
Dim type = Common.Reflection.GetEntityTypeFromString(expression.Target.Name, True)
If Not type Is Nothing Then
If type.GetInterfaces.Contains(GetType(ISoftDelete)) Then
Dim table = DirectCast(expression.Target.ElementType, Entity.Core.Metadata.Edm.EntityType)
If table.Properties.Any(Function(p) p.Name = SoftDeleteColumnName) Then
Dim binding = expression.Bind()
Return binding.Filter(binding.VariableType.Variable(binding.VariableName).Property(SoftDeleteColumnName).Equal(DbExpression.FromBoolean(False)))
End If
End If
End If
Return MyBase.Visit(expression)
End Function
End Class
这就是诀窍。否#34;已弃用"从数据库中查询记录。但经过几次查询后,抛出了非常深的异常。当我然后通过Entity Framework检查生成的查询时,我的头痛变得更糟(为了便于阅读,我删除了大多数where子句):
SELECT TOP (1)
[Extent1].[AddressID] AS [AddressID],
[Extent1].[LinkedEntityID] AS [LinkedEntityID],
[Extent1].[Type] AS [Type],
[Extent1].[Status] AS [Status],
[Extent1].[AddressLine1] AS [AddressLine1],
[Extent1].[AddressLine2] AS [AddressLine2],
[Extent1].[PostalCode] AS [PostalCode],
[Extent1].[City] AS [City],
[Extent1].[State] AS [State],
[Extent1].[Country] AS [Country],
[Extent1].[Description] AS [Description],
[Extent1].[CreatedAt] AS [CreatedAt],
[Extent1].[CreatedBy] AS [CreatedBy],
[Extent1].[ModifiedAt] AS [ModifiedAt],
[Extent1].[ModifiedBy] AS [ModifiedBy],
[Extent1].[RowVersion] AS [RowVersion],
[Extent1].[Deprecated] AS [Deprecated],
[Extent1].[IsPrimary] AS [IsPrimary]
FROM ( SELECT [Var_130].[AddressID] AS [AddressID], [Var_130].[LinkedEntityID] AS [LinkedEntityID], [Var_130].[Type] AS [Type], [Var_130].[Status] AS [Status], [Var_130].[IsPrimary] AS [IsPrimary], [Var_130].[AddressLine1] AS [AddressLine1], [Var_130].[AddressLine2] AS [AddressLine2], [Var_130].[PostalCode] AS [PostalCode], [Var_130].[City] AS [City], [Var_130].[State] AS [State], [Var_130].[Country] AS [Country], [Var_130].[Description] AS [Description], [Var_130].[CreatedAt] AS [CreatedAt], [Var_130].[CreatedBy] AS [CreatedBy], [Var_130].[ModifiedAt] AS [ModifiedAt], [Var_130].[ModifiedBy] AS [ModifiedBy], [Var_130].[RowVersion] AS [RowVersion], [Var_130].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_131].[AddressID] AS [AddressID], [Var_131].[LinkedEntityID] AS [LinkedEntityID], [Var_131].[Type] AS [Type], [Var_131].[Status] AS [Status], [Var_131].[IsPrimary] AS [IsPrimary], [Var_131].[AddressLine1] AS [AddressLine1], [Var_131].[AddressLine2] AS [AddressLine2], [Var_131].[PostalCode] AS [PostalCode], [Var_131].[City] AS [City], [Var_131].[State] AS [State], [Var_131].[Country] AS [Country], [Var_131].[Description] AS [Description], [Var_131].[CreatedAt] AS [CreatedAt], [Var_131].[CreatedBy] AS [CreatedBy], [Var_131].[ModifiedAt] AS [ModifiedAt], [Var_131].[ModifiedBy] AS [ModifiedBy], [Var_131].[RowVersion] AS [RowVersion], [Var_131].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_132].[AddressID] AS [AddressID], [Var_132].[LinkedEntityID] AS [LinkedEntityID], [Var_132].[Type] AS [Type], [Var_132].[Status] AS [Status], [Var_132].[IsPrimary] AS [IsPrimary], [Var_132].[AddressLine1] AS [AddressLine1], [Var_132].[AddressLine2] AS [AddressLine2], [Var_132].[PostalCode] AS [PostalCode], [Var_132].[City] AS [City], [Var_132].[State] AS [State], [Var_132].[Country] AS [Country], [Var_132].[Description] AS [Description], [Var_132].[CreatedAt] AS [CreatedAt], [Var_132].[CreatedBy] AS [CreatedBy], [Var_132].[ModifiedAt] AS [ModifiedAt], [Var_132].[ModifiedBy] AS [ModifiedBy], [Var_132].[RowVersion] AS [RowVersion], [Var_132].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_133].[AddressID] AS [AddressID], [Var_133].[LinkedEntityID] AS [LinkedEntityID], [Var_133].[Type] AS [Type], [Var_133].[Status] AS [Status], [Var_133].[IsPrimary] AS [IsPrimary], [Var_133].[AddressLine1] AS [AddressLine1], [Var_133].[AddressLine2] AS [AddressLine2], [Var_133].[PostalCode] AS [PostalCode], [Var_133].[City] AS [City], [Var_133].[State] AS [State], [Var_133].[Country] AS [Country], [Var_133].[Description] AS [Description], [Var_133].[CreatedAt] AS [CreatedAt], [Var_133].[CreatedBy] AS [CreatedBy], [Var_133].[ModifiedAt] AS [ModifiedAt], [Var_133].[ModifiedBy] AS [ModifiedBy], [Var_133].[RowVersion] AS [RowVersion], [Var_133].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_134].[AddressID] AS [AddressID], [Var_134].[LinkedEntityID] AS [LinkedEntityID], [Var_134].[Type] AS [Type], [Var_134].[Status] AS [Status], [Var_134].[IsPrimary] AS [IsPrimary], [Var_134].[AddressLine1] AS [AddressLine1], [Var_134].[AddressLine2] AS [AddressLine2], [Var_134].[PostalCode] AS [PostalCode], [Var_134].[City] AS [City], [Var_134].[State] AS [State], [Var_134].[Country] AS [Country], [Var_134].[Description] AS [Description], [Var_134].[CreatedAt] AS [CreatedAt], [Var_134].[CreatedBy] AS [CreatedBy], [Var_134].[ModifiedAt] AS [ModifiedAt], [Var_134].[ModifiedBy] AS [ModifiedBy], [Var_134].[RowVersion] AS [RowVersion], [Var_134].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_135].[AddressID] AS [AddressID], [Var_135].[LinkedEntityID] AS [LinkedEntityID], [Var_135].[Type] AS [Type], [Var_135].[Status] AS [Status], [Var_135].[IsPrimary] AS [IsPrimary], [Var_135].[AddressLine1] AS [AddressLine1], [Var_135].[AddressLine2] AS [AddressLine2], [Var_135].[PostalCode] AS [PostalCode], [Var_135].[City] AS [City], [Var_135].[State] AS [State], [Var_135].[Country] AS [Country], [Var_135].[Description] AS [Description], [Var_135].[CreatedAt] AS [CreatedAt], [Var_135].[CreatedBy] AS [CreatedBy], [Var_135].[ModifiedAt] AS [ModifiedAt], [Var_135].[ModifiedBy] AS [ModifiedBy], [Var_135].[RowVersion] AS [RowVersion], [Var_135].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_136].[AddressID] AS [AddressID], [Var_136].[LinkedEntityID] AS [LinkedEntityID], [Var_136].[Type] AS [Type], [Var_136].[Status] AS [Status], [Var_136].[IsPrimary] AS [IsPrimary], [Var_136].[AddressLine1] AS [AddressLine1], [Var_136].[AddressLine2] AS [AddressLine2], [Var_136].[PostalCode] AS [PostalCode], [Var_136].[City] AS [City], [Var_136].[State] AS [State], [Var_136].[Country] AS [Country], [Var_136].[Description] AS [Description], [Var_136].[CreatedAt] AS [CreatedAt], [Var_136].[CreatedBy] AS [CreatedBy], [Var_136].[ModifiedAt] AS [ModifiedAt], [Var_136].[ModifiedBy] AS [ModifiedBy], [Var_136].[RowVersion] AS [RowVersion], [Var_136].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_137].[AddressID] AS [AddressID], [Var_137].[LinkedEntityID] AS [LinkedEntityID], [Var_137].[Type] AS [Type], [Var_137].[Status] AS [Status], [Var_137].[IsPrimary] AS [IsPrimary], [Var_137].[AddressLine1] AS [AddressLine1], [Var_137].[AddressLine2] AS [AddressLine2], [Var_137].[PostalCode] AS [PostalCode], [Var_137].[City] AS [City], [Var_137].[State] AS [State], [Var_137].[Country] AS [Country], [Var_137].[Description] AS [Description], [Var_137].[CreatedAt] AS [CreatedAt], [Var_137].[CreatedBy] AS [CreatedBy], [Var_137].[ModifiedAt] AS [ModifiedAt], [Var_137].[ModifiedBy] AS [ModifiedBy], [Var_137].[RowVersion] AS [RowVersion], [Var_137].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_138].[AddressID] AS [AddressID], [Var_138].[LinkedEntityID] AS [LinkedEntityID], [Var_138].[Type] AS [Type], [Var_138].[Status] AS [Status], [Var_138].[IsPrimary] AS [IsPrimary], [Var_138].[AddressLine1] AS [AddressLine1], [Var_138].[AddressLine2] AS [AddressLine2], [Var_138].[PostalCode] AS [PostalCode], [Var_138].[City] AS [City], [Var_138].[State] AS [State], [Var_138].[Country] AS [Country], [Var_138].[Description] AS [Description], [Var_138].[CreatedAt] AS [CreatedAt], [Var_138].[CreatedBy] AS [CreatedBy], [Var_138].[ModifiedAt] AS [ModifiedAt], [Var_138].[ModifiedBy] AS [ModifiedBy], [Var_138].[RowVersion] AS [RowVersion], [Var_138].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_139].[AddressID] AS [AddressID], [Var_139].[LinkedEntityID] AS [LinkedEntityID], [Var_139].[Type] AS [Type], [Var_139].[Status] AS [Status], [Var_139].[IsPrimary] AS [IsPrimary], [Var_139].[AddressLine1] AS [AddressLine1], [Var_139].[AddressLine2] AS [AddressLine2], [Var_139].[PostalCode] AS [PostalCode], [Var_139].[City] AS [City], [Var_139].[State] AS [State], [Var_139].[Country] AS [Country], [Var_139].[Description] AS [Description], [Var_139].[CreatedAt] AS [CreatedAt], [Var_139].[CreatedBy] AS [CreatedBy], [Var_139].[ModifiedAt] AS [ModifiedAt], [Var_139].[ModifiedBy] AS [ModifiedBy], [Var_139].[RowVersion] AS [RowVersion], [Var_139].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_140].[AddressID] AS [AddressID], [Var_140].[LinkedEntityID] AS [LinkedEntityID], [Var_140].[Type] AS [Type], [Var_140].[Status] AS [Status], [Var_140].[IsPrimary] AS [IsPrimary], [Var_140].[AddressLine1] AS [AddressLine1], [Var_140].[AddressLine2] AS [AddressLine2], [Var_140].[PostalCode] AS [PostalCode], [Var_140].[City] AS [City], [Var_140].[State] AS [State], [Var_140].[Country] AS [Country], [Var_140].[Description] AS [Description], [Var_140].[CreatedAt] AS [CreatedAt], [Var_140].[CreatedBy] AS [CreatedBy], [Var_140].[ModifiedAt] AS [ModifiedAt], [Var_140].[ModifiedBy] AS [ModifiedBy], [Var_140].[RowVersion] AS [RowVersion], [Var_140].[Deprecated] AS [Deprecated]
FROM ( SELECT [Var_141].[AddressID] AS [AddressID], [Var_141].[LinkedEntityID] AS [LinkedEntityID], [Var_141].[Type] AS [Type], [Var_141].[Status] AS [Status], [Var_141].[IsPrimary] AS [IsPrimary], [Var_141].[AddressLine1] AS [AddressLine1], [Var_141].[AddressLine2] AS [AddressLine2], [Var_141].[PostalCode] AS [PostalCode], [Var_141].[City] AS [City], [Var_141].[State] AS [State], [Var_141].[Country] AS [Country], [Var_141].[Description] AS [Description], [Var_141].[CreatedAt] AS [CreatedAt], [Var_141].[CreatedBy] AS [CreatedBy], [Var_141].[ModifiedAt] AS [ModifiedAt], [Var_141].[ModifiedBy] AS [ModifiedBy], [Var_141].[RowVersion] AS [RowVersion], [Var_141].[Deprecated] AS [Deprecated]
) AS [Var_142]
WHERE [Var_142].[Deprecated] = 0
) AS [Var_141]
WHERE [Var_141].[Deprecated] = 0
) AS [Var_140]
WHERE [Var_140].[Deprecated] = 0
) AS [Var_139]
WHERE [Var_139].[Deprecated] = 0
) AS [Var_138]
WHERE [Var_138].[Deprecated] = 0
) AS [Var_137]
WHERE [Var_137].[Deprecated] = 0
) AS [Var_136]
WHERE [Var_136].[Deprecated] = 0
) AS [Var_135]
WHERE [Var_135].[Deprecated] = 0
) AS [Var_134]
WHERE [Var_134].[Deprecated] = 0
) AS [Var_133]
WHERE [Var_133].[Deprecated] = 0
) AS [Var_132]
WHERE [Var_132].[Deprecated] = 0
) AS [Var_131]
WHERE [Var_131].[Deprecated] = 0
) AS [Var_130]
WHERE [Var_130].[Deprecated] = 0
) AS [Extent1]
WHERE [Extent1].[AddressID] = '381a335b-0992-44aa-bee3-21088d640a6c'
这是一种非常复杂的实现方式" WHERE Depricated = 0"显然不行!有谁知道我的代码和EF6会发生什么?
非常感谢提前!