Пример #1
0
        public virtual MigrationOperation CreateDeleteOperation(string migrationId)
        {
            DebugCheck.NotEmpty(migrationId);

            using (var connection = CreateConnection())
            {
                using (var context = CreateContext(connection))
                {
                    var historyRow
                        = new HistoryRow
                        {
                        MigrationId = migrationId,
                        ContextKey  = _contextKey
                        };

                    context.History.Attach(historyRow);
                    context.History.Remove(historyRow);

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return(new HistoryOperation(commandTracer.DbCommands));
                    }
                }
            }
        }
Пример #2
0
        public void Generate_can_output_insert_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                historyContext.History.Add(
                    new HistoryRow
                {
                    MigrationId    = "House Lannister",
                    ContextKey     = "The pointy end",
                    Model          = new byte[0],
                    ProductVersion = "Awesomeness"
                });

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var insertHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                          .Generate(new[] { insertHistoryOperation }, "4.0")
                          .Single();

                    Assert.Equal(@"INSERT [__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'House Lannister', N'The pointy end',  0x , N'Awesomeness')", sql.Sql.Trim());
                }
            }
        }
Пример #3
0
        public virtual MigrationOperation CreateInsertOperation(string migrationId, XDocument model)
        {
            DebugCheck.NotEmpty(migrationId);
            DebugCheck.NotNull(model);

            using (var connection = CreateConnection())
            {
                using (var context = CreateContext(connection))
                {
                    context.History.Add(
                        new HistoryRow
                    {
                        MigrationId    = migrationId,
                        ContextKey     = _contextKey,
                        Model          = new ModelCompressor().Compress(model),
                        ProductVersion = _productVersion
                    });

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return(new HistoryOperation(commandTracer.DbCommands));
                    }
                }
            }
        }
Пример #4
0
        public void Generate_can_output_delete_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                var historyRow
                    = new HistoryRow
                    {
                    MigrationId = "House Lannister",
                    ContextKey  = "The pointy end"
                    };

                historyContext.History.Attach(historyRow);
                historyContext.History.Remove(historyRow);

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var deleteHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                          .Generate(new[] { deleteHistoryOperation }, "4.0")
                          .Single();

                    Assert.Equal(@"DELETE [__MigrationHistory]
WHERE (([MigrationId] = N'House Lannister') AND ([ContextKey] = N'The pointy end'))", sql.Sql.Trim());
                }
            }
        }
        private IEnumerable <TCommandTree> Generate <TCommandTree>(string entityIdentity, EntityState state)
            where TCommandTree : DbCommandTree
        {
            DebugCheck.NotEmpty(entityIdentity);

            var entityType
                = _metadataWorkspace
                  .GetItem <EntityType>(entityIdentity, DataSpace.CSpace);

            using (var context = CreateContext())
            {
                var set    = context.Set(entityType.GetClrType());
                var entity = set.Create();

                InstantiateNullableKeys(entity, entityType);
                InstantiateComplexProperties(entity, entityType.Properties);

                set.Attach(entity);
                context.Entry(entity).State = state;

                ChangeRelationshipStates(context, entityType, entity, state);

                using (var commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    foreach (var commandTree in commandTracer.CommandTrees)
                    {
                        yield return((TCommandTree)commandTree);
                    }
                }
            }
        }
Пример #6
0
        public virtual IEnumerable <DbQueryCommandTree> CreateDiscoveryQueryTrees()
        {
            DbConnection connection = (DbConnection)null;

            try
            {
                connection = this.CreateConnection();
                foreach (string schema in this._schemas)
                {
                    using (HistoryContext context = this.CreateContext(connection, schema))
                    {
                        IOrderedQueryable <string> query   = context.History.Where <HistoryRow>((Expression <Func <HistoryRow, bool> >)(h => h.ContextKey == this._contextKey)).Select <HistoryRow, string>((Expression <Func <HistoryRow, string> >)(s => s.MigrationId)).OrderByDescending <string, string>((Expression <Func <string, string> >)(s => s));
                        DbQuery <string>           dbQuery = query as DbQuery <string>;
                        if (dbQuery != null)
                        {
                            dbQuery.InternalQuery.ObjectQuery.EnablePlanCaching = false;
                        }
                        using (CommandTracer commandTracer = new CommandTracer((DbContext)context))
                        {
                            query.First <string>();
                            DbQueryCommandTree queryTree = commandTracer.CommandTrees.OfType <DbQueryCommandTree>().Single <DbQueryCommandTree>((Func <DbQueryCommandTree, bool>)(t => t.DataSpace == DataSpace.SSpace));
                            yield return(new DbQueryCommandTree(queryTree.MetadataWorkspace, queryTree.DataSpace, queryTree.Query.Accept <DbExpression>((DbExpressionVisitor <DbExpression>) new HistoryRepository.ParameterInliner(commandTracer.DbCommands.Single <DbCommand>().Parameters))));
                        }
                    }
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
        public virtual MigrationOperation CreateDeleteOperation(string migrationId)
        {
            DebugCheck.NotEmpty(migrationId);

            using (var connection = CreateConnection())
            {
                using (var context = CreateContext(connection))
                {
                    var historyRow
                        = new HistoryRow
                        {
                        MigrationId = migrationId.RestrictTo(_migrationIdMaxLength),
                        ContextKey  = _contextKey
                        };

                    context.History.Attach(historyRow);
                    context.History.Remove(historyRow);

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return(new HistoryOperation(
                                   commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList()));
                    }
                }
            }
        }
Пример #8
0
        public virtual MigrationOperation CreateDeleteOperation(string migrationId)
        {
            DbConnection connection = (DbConnection)null;

            try
            {
                connection = this.CreateConnection();
                using (HistoryContext context = this.CreateContext(connection, (string)null))
                {
                    HistoryRow entity = new HistoryRow()
                    {
                        MigrationId = migrationId.RestrictTo(this._migrationIdMaxLength),
                        ContextKey  = this._contextKey
                    };
                    context.History.Attach(entity);
                    context.History.Remove(entity);
                    using (CommandTracer commandTracer = new CommandTracer((DbContext)context))
                    {
                        context.SaveChanges();
                        return((MigrationOperation) new HistoryOperation((IList <DbModificationCommandTree>)commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList <DbModificationCommandTree>(), (object)null));
                    }
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
Пример #9
0
        public virtual MigrationOperation CreateInsertOperation(
            string migrationId,
            VersionedModel versionedModel)
        {
            DbConnection connection = (DbConnection)null;

            try
            {
                connection = this.CreateConnection();
                using (HistoryContext context = this.CreateContext(connection, (string)null))
                {
                    context.History.Add(new HistoryRow()
                    {
                        MigrationId    = migrationId.RestrictTo(this._migrationIdMaxLength),
                        ContextKey     = this._contextKey,
                        Model          = new ModelCompressor().Compress(versionedModel.Model),
                        ProductVersion = versionedModel.Version ?? HistoryRepository._productVersion
                    });
                    using (CommandTracer commandTracer = new CommandTracer((DbContext)context))
                    {
                        context.SaveChanges();
                        return((MigrationOperation) new HistoryOperation((IList <DbModificationCommandTree>)commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList <DbModificationCommandTree>(), (object)null));
                    }
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
Пример #10
0
        private IEnumerable <TCommandTree> GenerateAssociation <TCommandTree>(
            string associationIdentity,
            EntityState state)
            where TCommandTree : DbCommandTree
        {
            AssociationType associationType = this._metadataWorkspace.GetItem <AssociationType>(associationIdentity, DataSpace.CSpace);

            using (DbContext context = this.CreateContext())
            {
                EntityType         sourceEntityType   = associationType.SourceEnd.GetEntityType();
                object             sourceEntity       = this.InstantiateAndAttachEntity(sourceEntityType, context);
                EntityType         targetEntityType   = associationType.TargetEnd.GetEntityType();
                object             targetEntity       = sourceEntityType.GetRootType() == targetEntityType.GetRootType() ? sourceEntity : this.InstantiateAndAttachEntity(targetEntityType, context);
                ObjectStateManager objectStateManager = ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager;
                objectStateManager.ChangeRelationshipState(sourceEntity, targetEntity, associationType.FullName, associationType.TargetEnd.Name, state == EntityState.Deleted ? state : EntityState.Added);
                using (CommandTracer commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();
                    foreach (DbCommandTree commandTree in commandTracer.CommandTrees)
                    {
                        yield return((TCommandTree)commandTree);
                    }
                }
            }
        }
Пример #11
0
        private IEnumerable <DbModificationCommandTree> Generate(
            string entityIdentity,
            EntityState state)
        {
            EntityType entityType = this._metadataWorkspace.GetItem <EntityType>(entityIdentity, DataSpace.CSpace);

            using (DbContext context = this.CreateContext())
            {
                object entity = this.InstantiateAndAttachEntity(entityType, context);
                if (state != EntityState.Deleted)
                {
                    context.Entry(entity).State = state;
                }
                this.ChangeRelationshipStates(context, entityType, entity, state);
                if (state == EntityState.Deleted)
                {
                    context.Entry(entity).State = state;
                }
                this.HandleTableSplitting(context, entityType, entity, state);
                using (CommandTracer commandTracer = new CommandTracer(context))
                {
                    ((IObjectContextAdapter)context).ObjectContext.SaveChanges(SaveOptions.None);
                    foreach (DbCommandTree commandTree in commandTracer.CommandTrees)
                    {
                        yield return((DbModificationCommandTree)commandTree);
                    }
                }
            }
        }
        private IEnumerable <TCommandTree> GenerateAssociation <TCommandTree>(string associationIdentity, EntityState state)
            where TCommandTree : DbCommandTree
        {
            DebugCheck.NotEmpty(associationIdentity);

            var associationType
                = _metadataWorkspace
                  .GetItem <AssociationType>(associationIdentity, DataSpace.CSpace);

            using (var context = CreateContext())
            {
                var sourceEntityType = associationType.SourceEnd.GetEntityType();
                var sourceSet        = context.Set(sourceEntityType.GetClrType());
                var sourceEntity     = sourceSet.Create();

                InstantiateNullableKeys(sourceEntity, sourceEntityType);
                InstantiateComplexProperties(sourceEntity, sourceEntityType.Properties);

                sourceSet.Attach(sourceEntity);

                var targetEntityType = associationType.TargetEnd.GetEntityType();
                var targetSet        = context.Set(targetEntityType.GetClrType());
                var targetEntity     = targetSet.Create();

                InstantiateNullableKeys(targetEntity, targetEntityType);
                InstantiateComplexProperties(targetEntity, targetEntityType.Properties);

                targetSet.Attach(targetEntity);

                var objectStateManager
                    = ((IObjectContextAdapter)context)
                      .ObjectContext
                      .ObjectStateManager;

                objectStateManager
                .ChangeRelationshipState(
                    sourceEntity,
                    targetEntity,
                    associationType.FullName,
                    associationType.TargetEnd.Name,
                    state == EntityState.Deleted ? state : EntityState.Added
                    );

                using (var commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    foreach (var commandTree in commandTracer.CommandTrees)
                    {
                        yield return((TCommandTree)commandTree);
                    }
                }
            }
        }
Пример #13
0
        public virtual IEnumerable <DbQueryCommandTree> CreateDiscoveryQueryTrees()
        {
            DbConnection connection = null;

            try
            {
                connection = CreateConnection();

                foreach (var schema in _schemas)
                {
                    using (var context = CreateContext(connection, schema))
                    {
                        var query
                            = context.History
                              .Where(h => h.ContextKey == _contextKey)
                              .Select(s => s.MigrationId)
                              .OrderByDescending(s => s);

                        var dbQuery = query as DbQuery <string>;

                        if (dbQuery != null)
                        {
                            dbQuery.InternalQuery.ObjectQuery.EnablePlanCaching = false;
                        }

                        using (var commandTracer = new CommandTracer(context))
                        {
                            query.First();

                            var queryTree
                                = commandTracer
                                  .CommandTrees
                                  .OfType <DbQueryCommandTree>()
                                  .Single(t => t.DataSpace == DataSpace.SSpace);

                            yield return
                                (new DbQueryCommandTree(
                                     queryTree.MetadataWorkspace,
                                     queryTree.DataSpace,
                                     queryTree.Query.Accept(
                                         new ParameterInliner(
                                             commandTracer.DbCommands.Single().Parameters))));
                        }
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }
        protected AbstractDispatchTestBase(Action <ICommandRegistry, CustomDispatcher> registrations)
        {
            var serviceCollection = new ServiceCollection();
            var resolver          = new CommandingDependencyResolverAdapter(
                (type, instance) => serviceCollection.AddSingleton(type, instance),
                (type, impl) => serviceCollection.AddTransient(type, impl),
                type => ServiceProvider.GetService(type)
                );

            CommandingConfiguration = new CommandingRuntime();
            var registry = CommandingConfiguration.AddCommanding(resolver);

            CommandTracer = new CommandTracer();
            serviceCollection.AddSingleton(CommandTracer);
            CustomDispatcher = new CustomDispatcher();
            CustomExecuter   = new CustomExecuter();

            registrations(registry, CustomDispatcher);
            ServiceProvider = serviceCollection.BuildServiceProvider();
            Dispatcher      = ServiceProvider.GetRequiredService <ICommandDispatcher>();
        }
        private IEnumerable <DbModificationCommandTree> Generate(string entityIdentity, EntityState state)
        {
            DebugCheck.NotEmpty(entityIdentity);

            var entityType
                = _metadataWorkspace
                  .GetItem <EntityType>(entityIdentity, DataSpace.CSpace);

            using (var context = CreateContext())
            {
                var entity = InstantiateAndAttachEntity(entityType, context);

                if (state != EntityState.Deleted)
                {
                    // For deletes, we need to set the state
                    // _after_ dealing with IAs.
                    context.Entry(entity).State = state;
                }

                ChangeRelationshipStates(context, entityType, entity, state);

                if (state == EntityState.Deleted)
                {
                    context.Entry(entity).State = state;
                }

                HandleTableSplitting(context, entityType, entity, state);

                using (var commandTracer = new CommandTracer(context))
                {
                    ((IObjectContextAdapter)context).ObjectContext.SaveChanges(SaveOptions.None);

                    foreach (var commandTree in commandTracer.CommandTrees)
                    {
                        yield return((DbModificationCommandTree)commandTree);
                    }
                }
            }
        }
Пример #16
0
        public virtual MigrationOperation CreateInsertOperation(string migrationId, XDocument model)
        {
            DebugCheck.NotEmpty(migrationId);
            DebugCheck.NotNull(model);

            DbConnection connection = null;

            try
            {
                connection = CreateConnection();

                using (var context = CreateContext(connection))
                {
                    context.History.Add(
                        new HistoryRow
                    {
                        MigrationId    = migrationId.RestrictTo(_migrationIdMaxLength),
                        ContextKey     = _contextKey,
                        Model          = new ModelCompressor().Compress(model),
                        ProductVersion = _productVersion
                    });

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return(new HistoryOperation(
                                   commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList()));
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }