Esempio n. 1
0
 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
     base.OnConfiguring(optionsBuilder);
     SqliteConnectionStringBuilder sb = new SqliteConnectionStringBuilder();
     sb.DataSource = filename;
     optionsBuilder.UseSqlite(sb.ToString());
 }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "WeatherDressing.db" };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

            //optionsBuilder.UseSqlite(connection);
        }
Esempio n. 3
0
        // This method connects the context with the database
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "ef6NotesManagerV3.sqlite" };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

            optionsBuilder.UseSqlite(connection);
        }
Esempio n. 4
0
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = DATABASE_NAME };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

            optionsBuilder.UseSqlite(connection);
        }
Esempio n. 5
0
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "MQTTRawData.db" };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);
            Debug.WriteLine($"Connecting to:{connection.ConnectionString}");

            optionsBuilder.UseSqlite(connection);
        }
Esempio n. 6
0
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      var connStringBuilder = new SqliteConnectionStringBuilder
      { DataSource = Path.Combine(_appEnv.ApplicationBasePath, _data.Configuration["App:DbFile"]) };
      var connString = connStringBuilder.ToString();
      var conn = new SqliteConnection(connString);

      optionsBuilder.UseSqlite(conn);
    }
Esempio n. 7
0
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // Visual Studio 2015 | Use the LocalDb 12 instance created by Visual Studio
            //optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;");

            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test254.sqlite" };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);
            
            optionsBuilder.UseSqlite(connection);
        }
        public SqliteDatabaseProvider()
        {
            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" };
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

            var builder = new DbContextOptionsBuilder<TestDbContext>();
            builder.UseSqlite(connection);
            options = builder.Options;

            using (var context = new TestDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
            }
        }
Esempio n. 9
0
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var loggingService = Singleton<ServiceController>.Instance.Get<ILoggingService>();
            loggingService.WriteLine("Configuring database connection...");

            var connectionStringBuilder = new SqliteConnectionStringBuilder {DataSource = "scrooge.db"};
            var connectionString = connectionStringBuilder.ToString();
            var connection = new SqliteConnection(connectionString);

            loggingService.WriteLine("Using connection string: " + connectionString);

            optionsBuilder.UseSqlite(connection);

            base.OnConfiguring(optionsBuilder);

            loggingService.WriteLine("Database configured!");
        }
        public virtual SqliteRelationalConnection CreateReadOnlyConnection()
        {
            var builder = new SqliteConnectionStringBuilder(ConnectionString)
            {
                Mode = SqliteOpenMode.ReadOnly
            };

            var options = new DbContextOptionsBuilder();
            options.UseSqlite(builder.ToString());

            return new SqliteRelationalConnection(
                _rawSqlCommandBuilder,
                options.Options,
                (ILogger<SqliteRelationalConnection>)Logger);
        }
        public void ToString_builds_string()
        {
            var builder = new SqliteConnectionStringBuilder
                {
                    DataSource = "test.db",
                    CacheMode = CacheMode.Shared
                };
            Assert.Equal("Data Source=test.db;Cache=Shared", builder.ToString());

            Assert.Equal("Data Source=test2.db", new SqliteConnectionStringBuilder(" Data Source = test2.db ").ToString());
        }
        public void FullySetupRelationshipsTest(DatabaseType databaseType)
        {
            // Arrange
            // Create untracked entities equivaelent to our collection
            // and then attempt to save and check list
            var childEntities = new List<ChildEntity>
                {
                    new ChildEntity {Id = 1, Name = "ChildEntity 1", ParentEntityId = 1},
                    new ChildEntity {Id = default(long), Name = "ChildEntity 3", ParentEntityId = 1}
                };

            var childEntityReferencingChildEntities = new List<ChildEntityReferencingChildEntity>
                {
                    new ChildEntityReferencingChildEntity
                    {
                        Id = 1,
                        Name = "ChildEntityReferencingChildEntity 1",
                        ChildEntityId = 1,
                        ChildEntity = childEntities.Single(x => x.Id == 1),
                        ParentEntityId = 1
                    },
                    new ChildEntityReferencingChildEntity
                    {
                        Id = default(long),
                        Name = "ChildEntityReferencingChildEntity 3",
                        ChildEntityId = default(long),
                        ChildEntity = childEntities.Last(), // untracked and not yet added
                        ParentEntityId = 1
                    }
                };

            // If this relationship is already established then the entities must be added
            childEntities.First().ChildEntityReferencingChildEntities =
                childEntityReferencingChildEntities.Where(x => x.ChildEntityId == 1).ToList();

            // GraphDiff cannot handle this situation where a collection is added
            // Must have an existing Id or else EF fails.
            //childEntities.Last().ChildEntityReferencingChildEntities =
            //    childEntityReferencingChildEntities.Where(x => x.ChildEntityId == default(long)).ToList();

            var parentEntity = new ParentEntity
            {
                Id = 1,
                Name = "ParentEntity 1",
                ChildEntities = childEntities.Where(x => x.ParentEntityId == 1).ToList(),
                ChildReferencingChildEntities =
                    childEntityReferencingChildEntities.Where(x => x.ParentEntityId == 1).ToList()
            };

            // act
            var optionsBuilder = new DbContextOptionsBuilder<SimpleContext>();
            switch (databaseType)
            {
                case DatabaseType.InMemory:
                    optionsBuilder.UseInMemoryDatabase();
                    break;
                case DatabaseType.Sqlite:
                    var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test.db" };
                    var connectionString = connectionStringBuilder.ToString();
                    var connection = new SqliteConnection(connectionString);
                    optionsBuilder.UseSqlite(connection);

                    // Need to load in the Sqlite assemblies - overload assembly resolver
                    // http://stackoverflow.com/questions/7264383/options-for-using-system-data-sqlite-in-a-32bit-and-64bit-c-sharp-world
                    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
                    {
                        if (args.Name == "Sqlite3")
                        {
                            var path = Path.Combine(@"..\..\..\Lib\Sqlite3\", "Native");
                            path = Path.Combine(path, Environment.Is64BitProcess ? @"x64\" : @"x32\");
                            path = Path.Combine(path, "Sqlite3.dll");
                            return Assembly.LoadFrom(path);
                        }
                        return null;
                    };
                    break;

                case DatabaseType.SqlServer:
                    optionsBuilder.UseSqlServer(@"Server=(LocalDB)\MSSQLLocalDB;Database=Test;integrated security=True;");
                    break;
            }

            using (var context = new SimpleContext(optionsBuilder.Options))
            {
                // To ensure contents are empty delete contents for Sqlite
                context.Database.EnsureDeleted();

                // Ensure DB created
                context.Database.EnsureCreated();

                SimpleInitializer.Seed(context);
            } // must be seeded with a separate context or already added to graph

            using (var context = new SimpleContext(optionsBuilder.Options))
            {
                context.Update(parentEntity);

                context.SaveChanges();
            }

            // assert
            using (var context = new SimpleContext(optionsBuilder.Options))
            {
                var persistedParentEntity =
                    context.ParentEntities
                        .Include(x => x.ChildEntities)
                        .Include(x => x.ChildReferencingChildEntities)
                        .Single(x => x.Id == parentEntity.Id);

                Assert.That(persistedParentEntity.Id, Is.EqualTo(parentEntity.Id));
                Assert.That(persistedParentEntity.Name, Is.EqualTo(parentEntity.Name));
                Assert.That(persistedParentEntity.ChildEntities.Count, Is.EqualTo(parentEntity.ChildEntities.Count));
                Assert.That(persistedParentEntity.ChildReferencingChildEntities.Count,
                    Is.EqualTo(parentEntity.ChildReferencingChildEntities.Count));

                // Require to order by as EF 7 orders in the opposite order
                var zippedChildren =
                    persistedParentEntity.ChildEntities.OrderBy(x => x.Id).Zip(parentEntity.ChildEntities,
                        (persisted, entity) => new {Persisted = persisted, Entity = entity});

                foreach (var persistedAndEntity in zippedChildren)
                {
                    Assert.That(persistedAndEntity.Persisted.Name, Is.EqualTo(persistedAndEntity.Entity.Name));
                    Assert.That(persistedAndEntity.Persisted.ParentEntityId, Is.EqualTo(persistedAndEntity.Entity.ParentEntityId));
                }

                var zippedChildReferencingChildren =
                    persistedParentEntity.ChildReferencingChildEntities.OrderBy(x => x.Id).Zip(
                        parentEntity.ChildReferencingChildEntities,
                        (persisted, entity) => new {Persisted = persisted, Entity = entity});

                foreach (var persistedAndEntity in zippedChildReferencingChildren)
                {
                    Assert.That(persistedAndEntity.Persisted.Name, Is.EqualTo(persistedAndEntity.Entity.Name));
                    Assert.That(persistedAndEntity.Persisted.ParentEntityId, Is.EqualTo(persistedAndEntity.Entity.ParentEntityId));
                    Assert.That(persistedAndEntity.Persisted.ChildEntityId, Is.EqualTo(
                            persistedAndEntity.Entity.ChildEntityId == default(long) ?
                                3 : persistedAndEntity.Entity.ChildEntityId)); // should be added following ef graph merge
                }
            }
        }
        public void ToString_builds_minimal_string()
        {
            var builder = new SqliteConnectionStringBuilder
            {
                DataSource = "test.db"
            };

            Assert.Equal("Data Source=test.db", builder.ToString());
        }
        public void ToString_builds_string()
        {
            var builder = new SqliteConnectionStringBuilder
            {
                DataSource = "test.db",
                Cache = SqliteCacheMode.Shared,
                Mode = SqliteOpenMode.Memory
            };

            Assert.Equal("Data Source=test.db;Mode=Memory;Cache=Shared", builder.ToString());
        }