public void TestFixtureSetUp()
        {
            var localDb = new SqlLocalDbApiWrapper();

            if (!localDb.IsLocalDBInstalled())
            {
                throw new Exception("LocalDB is not installed!");
            }

            this.instance = TemporarySqlLocalDbInstance.Create(deleteFiles: true);

            // Initialize database
            var strategy = new DropCreateDatabaseAlways<SentinelContext>();
            Database.SetInitializer(strategy);

            var builder = this.instance.CreateConnectionStringBuilder();

            // Update the connection string to specify the name of the database
            // and its physical location to the current application directory
            builder.SetInitialCatalogName("SentinelAuth");
            builder.SetPhysicalFileName(@".\SentinelAuth.mdf");

            this.connectionString = builder.ConnectionString;

            using (var context = new SentinelContext(this.connectionString))
            {
                context.Database.Initialize(true);
            }
        }
        public void System_Data_SqlLocalDb_Assembly_Can_Be_Used_End_To_End_To_Trace_Create_And_Share_Instances()
        {
            Helpers.EnsureUserIsAdmin();

            SqlLocalDbApiWrapper target = new SqlLocalDbApiWrapper();

            if (!target.IsLocalDBInstalled())
            {
                Assert.Fail("SQL LocalDB is not installed.");
            }

            string instanceName = Guid.NewGuid().ToString();
            string version = target.LatestVersion;

            target.StartTracing();

            try
            {
                target.CreateInstance(instanceName, version);

                try
                {
                    target.StartInstance(instanceName);

                    try
                    {
                        string ownerSid;

                        using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
                        {
                            ownerSid = identity.User.Value;
                        }

                        string sharedInstanceName = Guid.NewGuid().ToString();

                        target.ShareInstance(ownerSid, instanceName, sharedInstanceName);
                        target.UnshareInstance(instanceName);
                    }
                    finally
                    {
                        target.StopInstance(instanceName, SqlLocalDbApi.StopTimeout);
                    }
                }
                finally
                {
                    target.DeleteInstance(instanceName);
                }
            }
            finally
            {
                target.StopTracing();
            }
        }
        public void System_Data_SqlLocalDb_Assembly_Can_Be_Used_End_To_End_To_Trace_Create_And_Share_Instances()
        {
            Helpers.EnsureUserIsAdmin();

            SqlLocalDbApiWrapper target = new SqlLocalDbApiWrapper();

            if (!target.IsLocalDBInstalled())
            {
                Assert.Fail("SQL LocalDB is not installed.");
            }

            string instanceName = Guid.NewGuid().ToString();
            string version      = target.LatestVersion;

            target.StartTracing();

            try
            {
                target.CreateInstance(instanceName, version);

                try
                {
                    target.StartInstance(instanceName);

                    try
                    {
                        string ownerSid;

                        using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
                        {
                            ownerSid = identity.User.Value;
                        }

                        string sharedInstanceName = Guid.NewGuid().ToString();

                        target.ShareInstance(ownerSid, instanceName, sharedInstanceName);
                        target.UnshareInstance(instanceName);
                    }
                    finally
                    {
                        target.StopInstance(instanceName, SqlLocalDbApi.StopTimeout);
                    }
                }
                finally
                {
                    target.DeleteInstance(instanceName);
                }
            }
            finally
            {
                target.StopTracing();
            }
        }
示例#4
0
        public LocalDb(string tempBasePath = @"C:\temp\", string instanceName = "v11.0")
        {
            InstanceName = instanceName;
            _tempBasePath = tempBasePath;

            ISqlLocalDbApi localDb = new SqlLocalDbApiWrapper();

            if (!localDb.IsLocalDBInstalled())
            {
                throw new SqlLocalDbException("LocalDb is not installed.");
            }

            CreateDirectoriesIfNotExists();

            DbName = RandomName();
            _attachDbFileName = Path.Combine(_tempBasePath, DbName) + ".mdf";
        }
        public void System_Data_SqlLocalDb_Assembly_Can_Be_Used_End_To_End_To_Create_And_Manage_Instances()
        {
            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Assert.Fail("SQL LocalDB is not installed.");
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            TestVersions(provider);

            TestInstances(provider);

            TestInstanceLifecycle(localDB, provider);
        }
        public void System_Data_SqlLocalDb_Assembly_Can_Be_Used_End_To_End_To_Create_And_Manage_Instances()
        {
            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Assert.Fail("SQL LocalDB is not installed.");
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            TestVersions(provider);

            TestInstances(provider);

            TestInstanceLifecycle(localDB, provider);
        }
示例#7
0
        internal static void Main(string[] args)
        {
            PrintBanner();

            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName);
                return;
            }

            if (args != null &&
                args.Length == 1 &&
                string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase))
            {
                SqlLocalDbApi.DeleteUserInstances(deleteFiles: true);
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            IList<ISqlLocalDbVersionInfo> versions = provider.GetVersions();

            Console.WriteLine(Strings.Program_VersionsListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbVersionInfo version in versions)
            {
                Console.WriteLine(version.Name);
            }

            Console.WriteLine();

            IList<ISqlLocalDbInstanceInfo> instances = provider.GetInstances();

            Console.WriteLine(Strings.Program_InstancesListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                Console.WriteLine(instanceInfo.Name);
            }

            Console.WriteLine();

            string instanceName = Guid.NewGuid().ToString();

            ISqlLocalDbInstance instance = provider.CreateInstance(instanceName);

            instance.Start();

            try
            {
                if (IsCurrentUserAdmin())
                {
                    instance.Share(Guid.NewGuid().ToString());
                }

                try
                {
                    using (SqlConnection connection = instance.CreateConnection())
                    {
                        connection.Open();

                        try
                        {
                            using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }

                            using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
                finally
                {
                    if (IsCurrentUserAdmin())
                    {
                        instance.Unshare();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                instance.Stop();
                localDB.DeleteInstance(instance.Name);
            }

            Console.WriteLine();
            Console.Write(Strings.Program_ExitPrompt);
            Console.ReadKey();
        }
示例#8
0
        internal static void Main(string[] args)
        {
            PrintBanner();

            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName);
                return;
            }

            if (args?.Length == 1 && string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase))
            {
                SqlLocalDbApi.DeleteUserInstances(deleteFiles: true);
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            IList <ISqlLocalDbVersionInfo> versions = provider.GetVersions();

            Console.WriteLine(Strings.Program_VersionsListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbVersionInfo version in versions)
            {
                Console.WriteLine(version.Name);
            }

            Console.WriteLine();

            IList <ISqlLocalDbInstanceInfo> instances = provider.GetInstances();

            Console.WriteLine(Strings.Program_InstancesListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                Console.WriteLine(instanceInfo.Name);
            }

            Console.WriteLine();

            string instanceName = Guid.NewGuid().ToString();

            ISqlLocalDbInstance instance = provider.CreateInstance(instanceName);

            instance.Start();

            try
            {
                if (IsCurrentUserAdmin())
                {
                    instance.Share(Guid.NewGuid().ToString());
                }

                try
                {
                    using (SqlConnection connection = instance.CreateConnection())
                    {
                        connection.Open();

                        try
                        {
                            using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }

                            using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
                finally
                {
                    if (IsCurrentUserAdmin())
                    {
                        instance.Unshare();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                instance.Stop();
                localDB.DeleteInstance(instance.Name);
            }

            Console.WriteLine();
            Console.Write(Strings.Program_ExitPrompt);
            Console.ReadKey();
        }