1

I have a test method that builds up a DataTable with a certain schema to use during the test. If I inline the code for the building up of the DataTable then the test runs in Debug Mode just fine. If I encapsulate it in a function it throws an AccessViolationException when the test is run in debug mode. When run in Release mode it succeeds without a problem. I have included the code below. I tried setting some of the compiler switches and test environment settings thinking they were the cause, but they had no effect. I am using MSTest and VS 2012 RC. The Test Project is .NET 4.5

[TestMethod]
        public void PkColumnSortedToTopWhenColumnMappingIsExisting()
        {
            // arrange
            var sourceMart = new TableMap();
            //var schema = new DataTable();

            var schema = GetDataBaseSchemaTable();


            schema.Rows.Add("Column1", 1, 2, 3, 4, true, /* isKey: */ false, true, "BaseColumn1", "BaseTable1", typeof(int), 24, true, false,
                                        false, false, false);
            schema.Rows.Add("Column2", 1, 2, 3, 4, true, /* isKey: */ false, true, "BaseColumn2", "BaseTable1", typeof(int), 24, true, false,
                                        false, false, false);
            var columnMapping1 = new ColumnMapping(schema.Rows[0], DatabaseType.MicrosoftSql);
            var columnMapping2 = new ColumnMapping(schema.Rows[1], DatabaseType.MicrosoftSql);
            sourceMart.Mappings.Add(columnMapping1);
            sourceMart.Mappings.Add(columnMapping2);
            var viewModel = new MappingViewModel(sourceMart);

            // Navigate to next page (first time building mappings)
            viewModel.Activate();

            // Set second column to be a PK
            sourceMart.Mappings.First(m => m.SourceColumnName == "Column2").IsDestinationColumnPartOfPrimaryKey = true;

            //Reactivate which simulates as if we navigated away and back which should resort it
            viewModel.Activate();

            // assert
            Assert.IsTrue(viewModel.ColumnMappingVMs.First().ColumnMapping.IsDestinationColumnPartOfPrimaryKey);
        }

        public DataTable GetDataBaseSchemaTable()
        {
            var schemaTable = new DataTable();
            schemaTable.Columns.Add(new DataColumn("ColumnName", typeof(string)));
            schemaTable.Columns.Add(new DataColumn("ColumnOrdinal", typeof(int)));
            schemaTable.Columns.Add(new DataColumn("ColumnSize", typeof(int)));
            schemaTable.Columns.Add(new DataColumn("SourceNumericPrecision", typeof(short)));
            schemaTable.Columns.Add(new DataColumn("SourceNumericScale", typeof(short)));
            schemaTable.Columns.Add(new DataColumn("IsUnique", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsKey", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsRowID", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("BaseColumnName", typeof(string)));
            schemaTable.Columns.Add(new DataColumn("BaseTableName", typeof(string)));
            schemaTable.Columns.Add(new DataColumn("DataType", typeof(Type)));
            schemaTable.Columns.Add(new DataColumn("ProviderType", typeof(int)));
            schemaTable.Columns.Add(new DataColumn("AllowDBNull", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsAliased", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsExpression", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsHidden", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsReadOnly", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsLong", typeof(bool)));

            return schemaTable;
        }
HintonBR
  • 76
  • 1
  • 6
  • 1
    According to [Microsoft](http://msdn.microsoft.com/en-us/library/system.accessviolationexception.aspx), the error occurs when you're attempting to read/write protected memory (or corrupt memory). The Microsoft link above has some interesting comments regarding this issue. There are also a decent amount of differences between [release and debug](http://stackoverflow.com/questions/5338733/what-is-the-difference-between-debug-mode-and-release-mode-in-visual-studio-2010) mode that might explain why it works in one and not the other – StoriKnow Aug 02 '12 at 19:08
  • Have you tried installing the official release of .Net 4.5? Also, have you tried enabling and disabling it's JIT optimization? – Earlz Sep 07 '12 at 20:23

0 Answers0