/// <summary>
 /// Initialize connection factory
 /// </summary>
 public virtual void InitConnectionFactory()
 {
     var connectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
     //TODO fix compilation warning (below)
     #pragma warning disable 0618
     Database.DefaultConnectionFactory = connectionFactory;
 }
Пример #2
0
        public void SqlCeConnectionFactory_with_properly_formed_environment_style_is_concatenated_correctly()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", @"|BuffyLovesAngel|", "");

            var connection = factory.CreateConnection("FakeDatabaseName");

            Assert.Equal(@"Data Source=|BuffyLovesAngel|FakeDatabaseName.sdf; ", connection.ConnectionString);
        }
        public DbContextTestsForSqlCe()
        {
            _previousConnectionFactory = DefaultConnectionFactoryResolver.Instance.ConnectionFactory;

            var sqlCeConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0",
                                                                    AppDomain.CurrentDomain.BaseDirectory, "");
            DefaultConnectionFactoryResolver.Instance.ConnectionFactory = sqlCeConnectionFactory;
        }
        public void SqlCeConnectionFactory_uses_database_name_as_sdf_filename_even_when_database_name_already_ends_in_sdf()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient");

            var connection = factory.CreateConnection("FakeDatabaseName.sdf");

            Assert.Equal("Data Source=|DataDirectory|FakeDatabaseName.sdf; ", connection.ConnectionString);
        }
Пример #5
0
        public void SqlCeConnectionFactory_treats_improperly_start_formed_environment_style_as_path_which_then_throws()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", @"AnyaLovesXander|", "");

            Assert.Equal(
                GenerateException(() => Path.Combine("|", "Willow")).Message,
                Assert.Throws <ArgumentException>(() => factory.CreateConnection("FakeDatabaseName")).Message);
        }
Пример #6
0
        public void SqlCeConnectionFactory_uses_database_name_as_sdf_filename_even_when_database_name_already_ends_in_sdf()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient");

            var connection = factory.CreateConnection("FakeDatabaseName.sdf");

            Assert.Equal("Data Source=|DataDirectory|FakeDatabaseName.sdf; ", connection.ConnectionString);
        }
 public void SqlCeConnectionFactory_throws_when_a_connection_with_bad_database_path_is_used()
 {
     var factory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", @"//C:::\\\D:\\D::::D\\\", "");
     using (var connection = factory.CreateConnection("FakeDatabaseName"))
     {
         Assert.Throws<NotSupportedException>(() => connection.Open()).ValidateMessage(typeof(File).Assembly,
                                                                                       "Argument_PathFormatNotSupported",
                                                                                       null);
     }
 }
        public void SqlCeConnectionFactory_uses_changed_database_path_when_creating_connection_string()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", @"C:\VicAndBobs\Novelty Island", "");

            Assert.Equal(@"C:\VicAndBobs\Novelty Island", factory.DatabaseDirectory);

            var connection = factory.CreateConnection("FakeDatabaseName");

            Assert.Equal(@"Data Source=C:\VicAndBobs\Novelty Island\FakeDatabaseName.sdf; ", connection.ConnectionString);
        }
Пример #9
0
        public void SqlCeConnectionFactory_uses_changed_database_path_when_creating_connection_string()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", @"C:\VicAndBobs\Novelty Island", "");

            Assert.Equal(@"C:\VicAndBobs\Novelty Island", factory.DatabaseDirectory);

            var connection = factory.CreateConnection("FakeDatabaseName");

            Assert.Equal(@"Data Source=C:\VicAndBobs\Novelty Island\FakeDatabaseName.sdf; ", connection.ConnectionString);
        }
Пример #10
0
        public void SqlCeConnectionFactory_uses_changed_base_connection_string_when_creating_connection_string()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", "", "Persist Security Info=False");

            Assert.Equal("Persist Security Info=False", factory.BaseConnectionString);

            var connection = factory.CreateConnection("FakeDatabaseName");

            Assert.Equal("Data Source=FakeDatabaseName.sdf; Persist Security Info=False", connection.ConnectionString);
        }
 public void SqlCeConnectionFactory_creates_a_SQL_CE_connection_from_a_database_name()
 {
     using (
         var connection =
             new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0").CreateConnection("FakeDatabaseName"))
     {
         var sqlCeExceptionType = _sqlCeAssembly.Value.GetType("System.Data.SqlServerCe.SqlCeConnection");
         Assert.IsType(sqlCeExceptionType, connection);
         Assert.Equal("Data Source=|DataDirectory|FakeDatabaseName.sdf; ", connection.ConnectionString);
     }
 }
     SqlCeConnectionFactory_creates_a_SQL_CE_connection_using_changed_database_path_and_base_connection_string()
 {
     var factory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", @"C:\VicAndBob\",
                                              "Persist Security Info=False");
     using (var connection = factory.CreateConnection("FakeDatabaseName"))
     {
         var sqlCeExceptionType = _sqlCeAssembly.Value.GetType("System.Data.SqlServerCe.SqlCeConnection");
         Assert.IsType(sqlCeExceptionType, connection);
         Assert.Equal(@"Data Source=C:\VicAndBob\FakeDatabaseName.sdf; Persist Security Info=False",
                      connection.ConnectionString);
     }
 }
Пример #13
0
        public void SqlCeConnectionFactory_throws_when_provider_returns_null_connection()
        {
            try
            {
                FakeSqlProviderFactory.Instance.ForceNullConnection = true;
                var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient");

                Assert.Equal(
                    Strings.DbContext_ProviderReturnedNullConnection,
                    Assert.Throws <InvalidOperationException>(() => factory.CreateConnection("FakeDatabaseName")).Message);
            }
            finally
            {
                FakeSqlProviderFactory.Instance.ForceNullConnection = false;
            }
        }
        public ActionResult EFCodeFirst()
        {
            int count;

            // this would usually go in Global.asax.cs in Application_Start
            {
                var factory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
                // for sql server you could do ...
                //var factory = new SqlConnectionFactory("Data Source=.;Initial Catalog=tempdb;Integrated Security=True");
                var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
                Database.DefaultConnectionFactory = profiled;
                Database.SetInitializer<EFContext>(new DropCreateDatabaseIfModelChanges<EFContext>());
            }
            // end

            EFContext context = null;
            using (MiniProfiler.Current.Step("EF Stuff"))
            {
                try
                {
                    using (MiniProfiler.Current.Step("Create Context"))
                        context = new EFContext();

                    using (MiniProfiler.Current.Step("First count"))
                        count = context.People.Count();

                    using (MiniProfiler.Current.Step("Insertion"))
                    {
                        var p = new Person { Name = "sam" };
                        context.People.Add(p);
                        context.SaveChanges();
                    }

                    using (MiniProfiler.Current.Step("Second count"))
                        count = context.People.Count();
                }
                finally
                {
                    if (context != null)
                    {
                        context.Dispose();
                    }
                }
            }

            return Json(count, JsonRequestBehavior.AllowGet);
        }
 public void SqlCeConnectionFactory_throws_when_a_base_connection_already_containing_a_data_source_is_used()
 {
     var factory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "",
                                              "Data Source=VicAndBobsDatabase.sdf");
     using (var connection = factory.CreateConnection("FakeDatabaseName"))
     {
         var sqlCeExceptionType = _sqlCeAssembly.Value.GetType("System.Data.SqlServerCe.SqlCeException");
         try
         {
             connection.Open();
             Assert.True(false);
         }
         catch (Exception ex)
         {
             Assert.IsType(sqlCeExceptionType, ex);
             Assert.True(ex.Message.Contains("VicAndBobsDatabase.sdf"));
         }
     }
 }
Пример #16
0
 /// <summary>
 /// Initialize connection factory
 /// </summary>
 public virtual void InitConnectionFactory()
 {
     var connectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
     Database.DefaultConnectionFactory = connectionFactory;
 }
        public void SqlCeConnectionFactory_sets_provider_invariant_name_to_value_from_long_constructor()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", "", "");

            Assert.Equal("System.Data.FakeSqlClient", factory.ProviderInvariantName);
        }
        public void SqlCeConnectionFactory_treats_improperly_start_formed_environment_style_as_path_which_then_throws()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", @"AnyaLovesXander|", "");

            Assert.Equal(
                GenerateException(() => Path.Combine("|", "Willow")).Message,
                Assert.Throws<ArgumentException>(() => factory.CreateConnection("FakeDatabaseName")).Message);
        }
        public void SqlCeConnectionFactory_throws_when_provider_returns_null_connection()
        {
            try
            {
                FakeSqlProviderFactory.Instance.ForceNullConnection = true;
                var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient");

                Assert.Equal(
                    Strings.DbContext_ProviderReturnedNullConnection,
                    Assert.Throws<InvalidOperationException>(() => factory.CreateConnection("FakeDatabaseName")).Message);
            }
            finally
            {
                FakeSqlProviderFactory.Instance.ForceNullConnection = false;
            }
        }
 public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_invalid_SqlCE_connection_string()
 {
     var sqlCeAssembly =
         new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0").CreateConnection("Dummy").GetType().Assembly;
     var context = new SimpleModelContextWithNoData("Data Sourc=Scenario_Use_AppConfig.sdf");
     Assert.Throws<ArgumentException>(() => GetObjectContext(context)).ValidateMessage(
         sqlCeAssembly,
         "ADP_KeywordNotSupported",
         "System.Data.SqlServerCe",
         "data sourc");
 }
        public void SqlCeConnectionFactory_uses_changed_base_connection_string_when_creating_connection_string()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", "", "Persist Security Info=False");

            Assert.Equal("Persist Security Info=False", factory.BaseConnectionString);

            var connection = factory.CreateConnection("FakeDatabaseName");

            Assert.Equal("Data Source=FakeDatabaseName.sdf; Persist Security Info=False", connection.ConnectionString);
        }
Пример #22
0
 private void InitDbCodeFirst()
 {
     string connectionString = ConfigurationManager.ConnectionStrings["DatabaseContext"].ConnectionString;
     var connectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "", connectionString);
     Database.DefaultConnectionFactory = connectionFactory;
     Database.SetInitializer<DatabaseContext>(new DropCreateDatabaseIfModelChanges<DatabaseContext>());
     //Database.SetInitializer<DatabaseContext>(new DropCreateDatabaseAlways<DatabaseContext>());
 }
        public void SqlCeConnectionFactory_throws_when_a_bad_base_connection_string_is_used()
        {
            var factory = new SqlCeConnectionFactory(
                "System.Data.SqlServerCe.4.0", "",
                "Whats On The End Of The Stick Vic=Admiral Nelsons Final Flannel");

            Assert.Throws<ArgumentException>(() => factory.CreateConnection("Something")).ValidateMessage(
                _sqlCeAssembly.Value, "ADP_KeywordNotSupported", null, "whats on the end of the stick vic");
        }
 void Initialize()
 {
     if (String.IsNullOrEmpty(MasterDatabaseDirectory)) throw new ApplicationException("MasterDatabaseDirectory cannot be null or empty");
     if (!Directory.Exists(MasterDatabaseDirectory))
     {
         Directory.CreateDirectory(MasterDatabaseDirectory);
         Directory.CreateDirectory(Path.Combine(MasterDatabaseDirectory, "locations"));
         Directory.CreateDirectory(Path.Combine(MasterDatabaseDirectory, "scenarios"));
     }
     //System.Data.Entity.Database.SetInitializer(new Initializer());
     var connection = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0").CreateConnection(Path.Combine(MasterDatabaseDirectory, "esme.db"));
     Context = new LocationContext(connection, true);
     Refresh();
     OnPropertyChanged("Locations");
     OnPropertyChanged("Scenarios");
 }
Пример #25
0
        public void SqlCeConnectionFactory_sets_provider_invariant_name_to_value_from_long_constructor()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", "", "");

            Assert.Equal("System.Data.FakeSqlClient", factory.ProviderInvariantName);
        }
        public void SqlCeConnectionFactory_with_properly_formed_environment_style_is_concatenated_correctly()
        {
            var factory = new SqlCeConnectionFactory("System.Data.FakeSqlClient", @"|BuffyLovesAngel|", "");

            var connection = factory.CreateConnection("FakeDatabaseName");

            Assert.Equal(@"Data Source=|BuffyLovesAngel|FakeDatabaseName.sdf; ", connection.ConnectionString);
        }
Пример #27
0
        public void DbContext_caches_models_for_two_providers()
        {
            // Ensure that the model is in use with a SQL connection
            using (var context = new SimpleModelContext())
            {
                context.Database.Initialize(force: false);
            }

            // Now try to use it with a CE connection; would throw in EF 4.1, will not now throw.
            var sqlCeConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0",
                                                                    AppDomain.CurrentDomain.BaseDirectory, "");
            using (var connection = sqlCeConnectionFactory.CreateConnection(DefaultDbName<SimpleModelContext>()))
            {
                using (var context = new SimpleModelContext(connection))
                {
                    context.Database.Initialize(force: false);
                }
            }
        }
Пример #28
0
        private static DbConnection CreateConnection()
        {
            if (!providerInitialized)
            {
                try
                {
                    using (var dataSet = (DataSet) System.Configuration.ConfigurationManager.GetSection("system.data"))
                    {
                        const string name = "Microsoft SQL Server Compact Data Provider";
                        const string invariant = "System.Data.SqlServerCe.4.0";
                        const string description = ".NET Framework Data Provider for Microsoft SQL Server Compact";
                        const string type =
                            "System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0";

                        DataRowCollection rows = dataSet.Tables[0].Rows;

                        if (!rows.Contains(invariant))
                            rows.Add(name, description, invariant, type);
                    }
                }
                catch (ConstraintException)
                {
                }
                providerInitialized = true;
            }

            var folder = AppDomain.CurrentDomain.BaseDirectory + @"\Database"; //"c:\\Temp";
            var databaseFile = "questions.sdf";
            var databaseName = "questions";

            var connectionStringBuilder = new SqlConnectionStringBuilder
                {
                    DataSource = Path.Combine(folder, databaseFile),
                    Password = "******"
                };

            var factory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", folder,
                                                     connectionStringBuilder.ToString());

            return factory.CreateConnection(databaseName);
        }