public void CacheCommittedWithTransactionTest()
        {
            string applicationName = "CacheWithinTransactionScopeTest";
            string cacheKey = "CacheWithinTransactionScopeTest.CacheKey";
            ICache cache = SpringContext.Current.GetObject<ICache>();

            using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
            using (TransactionScope transactionScope = new TransactionScope())
            {
                // create a new application to database
                Application application = new Application
                {
                    ApplicationName = applicationName,
                    LoweredApplicationName = applicationName.ToLowerInvariant(),
                    Description = ""
                };

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

                // save it into cache for 1 hour
                cache.Add(cacheKey, application, new TimeSpan(1, 0, 0), CachePriorityTypes.High);

                // the application should exist in both database and cache in the transaction
                Assert.AreEqual(1, ctx.Applications.Count(app => app.ApplicationName == applicationName), "The application should exist in database.");
                Assert.IsNotNull(cache.Get(cacheKey), "The application should exist in cache.");

                // complete the transaction
                transactionScope.Complete();
            }

            using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
            {
                // the application should exist in both database and cache in the transaction
                Assert.AreEqual(1, ctx.Applications.Count(app => app.ApplicationName == applicationName), "The application should exist in database.");
                Assert.IsNotNull(cache.Get(cacheKey), "The application should exist in cache.");

                // clear testing data.
                ctx.Applications.Delete(app => app.ApplicationName == applicationName);
                cache.Remove(cacheKey);
            }
        }
        private static Application CreateApplication(ApplicationObject applicationObject)
        {
            Application application = new Application { ExtensionDataTypeId = applicationObject.ExtensionDataTypeId };
            IEnumerator<KeyValuePair<string, object>> properties = applicationObject.GetFieldEnumerator();
            while (properties.MoveNext())
                application[properties.Current.Key] = properties.Current.Value;

            return application;
        }
        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();
            }
        }
 partial void DeleteApplication(Application instance);
 partial void UpdateApplication(Application instance);
 partial void InsertApplication(Application instance);