Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
  2. {
  3.     if (interceptionContext.Result.DataSpace == DataSpace.SSpace)
  4.     {
  5.         var insertCommand = interceptionContext.Result as DbInsertCommandTree;
  6.         if (insertCommand != null)
  7.         {
  8.             var column = ColumnAnnotationAttribute.GetColumnName<HierarchicalPathAttribute>(insertCommand.Target.VariableType.EdmType);
  9.             var clauses = insertCommand.SetClauses.ToList();
  10.             if (column != null)
  11.             {
  12.                 #region Sample SQL query
  13.                 /*
  14.                  * INSERT INTO Domain
  15.                  *       ([Id]
  16.                  *       ,[ParentId]
  17.                  *       ,[HierarchicalIdPath]
  18.                  *       ,[Name]
  19.                  *       ,[CreationDate]
  20.                  *       ,[CreationIdentity]
  21.                  *       ,[ModificationDate]
  22.                  *       ,[ModificationIdentity]
  23.                  *       ,[IsDeleted]
  24.                  *       ,[DeletionDate]
  25.                  *       ,[DeletionIdentity])
  26.                  * VALUES
  27.                  *       ('7E571111-7E7D-4FB3-A885-7F253E958933'
  28.                  *       ,'05CAB111-2D9F-49CF-910A-E009DAF01578'
  29.                  *       ,(
  30.                  *       SELECT HierarchicalIdPath FROM Domain
  31.                  *        WHERE Id = '05CAB111-2D9F-49CF-910A-E009DAF01578'
  32.                  *       ) + '7E571111-7E7D-4FB3-A885-7F253E958933'
  33.                  *       ,'NewDOM'
  34.                  *       ,'11/11/11 11:11'
  35.                  *       ,'root'
  36.                  *       ,'11/11/11 11:11'
  37.                  *       ,'root'
  38.                  *       ,0
  39.                  *       ,'11/11/11 11:11'
  40.                  *       ,'')
  41.                  */
  42.                 #endregion
  43.  
  44.                 clauses.AddOrReplaceClause(DbExpressionBuilder.SetClause(
  45.                     insertCommand.Target.Variable.Property(column),
  46.                     DbExpressionBuilder.NewCollection(insertCommand.Target.Expression).Select(
  47. //                            insertCommand.Target.Expression.Select(
  48.                         exp => insertCommand.Target.Variable
  49.                             .Property("HierarchicalIdPath")).Where(
  50.                                 exp => insertCommand.Target.Variable
  51.                                     .Property("ParentId")
  52.                                     .Equal(DbExpression.FromGuid(Guid.Parse("05cab111-2d9f-49cf-910a-e009daf01578")))).Take(DbExpressionBuilder.Constant(1))
  53.                     ));
  54.                 interceptionContext.Result = new DbInsertCommandTree(
  55.                     insertCommand.MetadataWorkspace,
  56.                     insertCommand.DataSpace,
  57.                     insertCommand.Target,
  58.                     clauses.AsReadOnly(),
  59.                     null);
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement