public void TwoDataContextSharesSingleConnection()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Global"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

            // MembershipDataContext creates an application using an opening connection
            Application application = null;
            string applicationName = "TwoDataContextSharesOpeningConnection";
            using (MembershipDataContext ctx = new MembershipDataContext(connection))
            {
                application = new Application
                {
                    ApplicationName = applicationName,
                    LoweredApplicationName = applicationName.ToLowerInvariant()
                };

                ctx.Applications.InsertOnSubmit(application);
                ctx.SubmitChanges();
            }

            Assert.AreEqual(ConnectionState.Open, connection.State);

            // ExtensionModelDataContext creates an object metadata using an opening connection
            using (ExtensionModelDataContext ctx = new ExtensionModelDataContext(connection))
            {
                ObjectMetadata objectMetadata = new ObjectMetadata
                {
                    ApplicationId = application.ApplicationId,
                    Category = "Linq2SQLDataContextTests.Category",
                    Name = "Linq2SQLDataContextTests.Name",
                    LastUpdatedOn = DateTime.UtcNow
                };

                ctx.ObjectMetadatas.InsertOnSubmit(objectMetadata);
                ctx.SubmitChanges();
            }

            Assert.AreEqual(ConnectionState.Open, connection.State);
            connection.Close();

            connection = new SqlConnection(connectionString);
            // MembershipDataContext removes an application using an closed connection
            using (MembershipDataContext ctx = new MembershipDataContext(connection))
            {
                application = ctx.Applications.FirstOrDefault(app => app.ApplicationName == applicationName);
                Assert.IsNotNull(application);
                ctx.Applications.DeleteOnSubmit(application);
                ctx.SubmitChanges();
            }

            Assert.AreEqual(ConnectionState.Closed, connection.State);

            // ExtensionModelDataContext removes an object metadata using an closed connection
            using (ExtensionModelDataContext ctx = new ExtensionModelDataContext(connection))
            {
                ObjectMetadata objectMetadata = ctx.ObjectMetadatas.FirstOrDefault(metadata => metadata.Name == "Linq2SQLDataContextTests.Name");
                Assert.IsNotNull(objectMetadata);
                ctx.ObjectMetadatas.DeleteOnSubmit(objectMetadata);
                ctx.SubmitChanges();
            }

            Assert.AreEqual(ConnectionState.Closed, connection.State);
        }
        public void TransactionScopeWithoutDTCTest()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Global"].ConnectionString;
            using (TransactionScope ts = new TransactionScope())
            {
                // creates an application using MembershipDataContext
                Application application = null;
                string applicationName = string.Format(CultureInfo.InvariantCulture, "TransactionScopeWithoutDTCTest.{0}", Guid.NewGuid());
                using (MembershipDataContext ctx = new MembershipDataContext(connectionString))
                {
                    application = new Application
                    {
                        ApplicationName = applicationName,
                        LoweredApplicationName = applicationName.ToLowerInvariant()
                    };

                    ctx.Applications.InsertOnSubmit(application);
                    ctx.SubmitChanges();

                }

                // creates an object metadata using ExtensionModelDataContext
                using (ExtensionModelDataContext ctx = new ExtensionModelDataContext(connectionString))
                {
                    Assert.AreEqual(ConnectionState.Closed, ctx.Connection.State);
                    ObjectMetadata objectMetadata = new ObjectMetadata
                    {
                        ApplicationId = application.ApplicationId,
                        Category = "TransactionScopeWithoutDTCTest.Category",
                        Name = "TransactionScopeWithoutDTCTest.Name",
                        LastUpdatedOn = DateTime.UtcNow
                    };

                    ctx.ObjectMetadatas.InsertOnSubmit(objectMetadata);
                    ctx.SubmitChanges();
                }

                // deletes above created temporary data using managed ado.net command.
                using (MembershipDataContext membershipDataContext = new MembershipDataContext())
                using (ExtensionModelDataContext extensionModelDataContext = new ExtensionModelDataContext())
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = string.Format(CultureInfo.InvariantCulture, "DELETE FROM {0} WHERE ApplicationId='{1}' AND Name='{2}'",
                        extensionModelDataContext.Mapping.GetTable(typeof(ObjectMetadata)).TableName,
                        application.ApplicationId,
                        "TransactionScopeWithoutDTCTest.Name");
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();

                    command.CommandText = string.Format(CultureInfo.InvariantCulture, "DELETE FROM {0} WHERE ApplicationId='{1}'",
                        membershipDataContext.Mapping.GetTable(typeof(Application)).TableName,
                        application.ApplicationId);
                    command.ExecuteNonQuery();
                }

                // gets the user "admin" by ASP.NET Membership
                Assert.IsNotNull(System.Web.Security.Membership.GetUser("admin"), "The user admin should exist.");

                ts.Complete();
            }
        }