Exemplo n.º 1
0
        public virtual DatabaseModel Clone()
        {            
            var clone = new DatabaseModel();
            var cloneContext = new CloneContext();

            clone._sequences.AddRange(Sequences.Select(s => s.Clone(cloneContext)));
            clone._tables.AddRange(Tables.Select(t => t.Clone(cloneContext)));

            return clone;
        }
Exemplo n.º 2
0
        public void GetSequence_finds_sequence_by_name()
        {
            var database = new DatabaseModel();
            var sequence0 = new Sequence("dbo.MySequence0");
            var sequence1 = new Sequence("dbo.MySequence1");

            database.AddSequence(sequence0);
            database.AddSequence(sequence1);

            Assert.Same(sequence0, database.GetSequence("dbo.MySequence0"));
            Assert.Same(sequence1, database.GetSequence("dbo.MySequence1"));
        }
Exemplo n.º 3
0
        public void AddSequence_adds_specified_sequence()
        {
            var database = new DatabaseModel();

            Assert.Equal(0, database.Sequences.Count);

            var sequence = new Sequence("dbo.MySequence");
            database.AddSequence(sequence);

            Assert.Equal(1, database.Sequences.Count);
            Assert.Same(sequence, database.Sequences[0]);
        }
Exemplo n.º 4
0
        public void Sequences_gets_read_only_list_of_sequences()
        {
            var database = new DatabaseModel();
            var sequence0 = new Sequence("dbo.MySequence0");
            var sequence1 = new Sequence("dbo.MySequence1");

            database.AddSequence(sequence0);
            database.AddSequence(sequence1);

            Assert.IsAssignableFrom<IReadOnlyList<Sequence>>(database.Sequences);
            Assert.Equal(2, database.Sequences.Count);
            Assert.Same(sequence0, database.Sequences[0]);
            Assert.Same(sequence1, database.Sequences[1]);
        }
Exemplo n.º 5
0
        public void Tables_gets_read_only_list_of_tables()
        {
            var database = new DatabaseModel();
            var table0 = new Table("dbo.MyTable0");
            var table1 = new Table("dbo.MyTable1");

            database.AddTable(table0);
            database.AddTable(table1);

            Assert.IsAssignableFrom<IReadOnlyList<Table>>(database.Tables);
            Assert.Equal(2, database.Tables.Count);
            Assert.Same(table0, database.Tables[0]);
            Assert.Same(table1, database.Tables[1]);
        }
Exemplo n.º 6
0
        public void RemoveSequence_removes_specified_sequence()
        {
            var database = new DatabaseModel();
            var sequence0 = new Sequence("dbo.MySequence0");
            var sequence1 = new Sequence("dbo.MySequence1");

            database.AddSequence(sequence0);
            database.AddSequence(sequence1);

            Assert.Equal(2, database.Sequences.Count);

            database.RemoveSequence("dbo.MySequence1");

            Assert.Equal(1, database.Sequences.Count);
            Assert.Same(sequence0, database.Sequences[0]);
        }
Exemplo n.º 7
0
        public virtual ModelDatabaseMapping GetMapping([NotNull] IModel model)
        {
            Check.NotNull(model, "model");

            return _mappingCache.GetOrAdd(model, m =>
                {
                    // TODO: Consider making this lazy since we don't want to load the whole model just to
                    // save changes to a single entity.
                    var database = new DatabaseModel();
                    var mapping = new ModelDatabaseMapping(m, database);

                    foreach (var entityType in m.EntityTypes)
                    {
                        var table = BuildTable(database, entityType);
                        mapping.Map(entityType, table);

                        foreach (var property in entityType.Properties)
                        {
                            mapping.Map(property, BuildColumn(table, property));
                        }

                        var primaryKey = entityType.GetKey();
                        mapping.Map(primaryKey, BuildPrimaryKey(database, primaryKey));

                        foreach (var index in entityType.Indexes)
                        {
                            mapping.Map(index, BuildIndex(database, index));
                        }
                    }

                    foreach (var entityType in m.EntityTypes)
                    {
                        foreach (var foreignKey in entityType.ForeignKeys)
                        {
                            mapping.Map(foreignKey, BuildForeignKey(database, foreignKey));
                        }
                    }

                    return mapping;
                });
        }
Exemplo n.º 8
0
        public void Clone_replicates_tables()
        {            
            var databaseModel = new DatabaseModel();
            var sequence0 = new Sequence("dbo.S0");
            var sequence1 = new Sequence("dbo.S1");
            var table0 = new Table("dbo.T0");
            var table1 = new Table("dbo.T1");

            databaseModel.AddSequence(sequence0);
            databaseModel.AddSequence(sequence1);
            databaseModel.AddTable(table0);
            databaseModel.AddTable(table1);

            var clone = databaseModel.Clone();

            Assert.NotSame(databaseModel, clone);
            Assert.Equal(2, clone.Sequences.Count);
            Assert.NotSame(sequence0, clone.Sequences[0]);
            Assert.NotSame(sequence1, clone.Sequences[1]);
            Assert.Equal("dbo.S0", clone.Sequences[0].Name);
            Assert.Equal("dbo.S1", clone.Sequences[1].Name);
            Assert.Equal(2, clone.Tables.Count);
            Assert.NotSame(table0, clone.Tables[0]);
            Assert.NotSame(table1, clone.Tables[1]);
            Assert.Equal("dbo.T0", clone.Tables[0].Name);
            Assert.Equal("dbo.T1", clone.Tables[1].Name);
        }
Exemplo n.º 9
0
        public void GetTable_finds_table_by_name()
        {
            var database = new DatabaseModel();
            var table0 = new Table("dbo.MyTable0");
            var table1 = new Table("dbo.MyTable1");

            database.AddTable(table0);
            database.AddTable(table1);

            Assert.Same(table0, database.GetTable("dbo.MyTable0"));
            Assert.Same(table1, database.GetTable("dbo.MyTable1"));
        }
        public virtual MigrationOperationSqlGenerator Create(DatabaseModel database)
        {
            Check.NotNull(database, "database");

            return new SQLiteMigrationOperationSqlGenerator(new SQLiteTypeMapper()) { Database = database };
        }
Exemplo n.º 11
0
        public void RemoveTable_removes_specified_table()
        {
            var database = new DatabaseModel();
            var table0 = new Table("dbo.MyTable0");
            var table1 = new Table("dbo.MyTable1");

            database.AddTable(table0);
            database.AddTable(table1);

            Assert.Equal(2, database.Tables.Count);

            database.RemoveTable("dbo.MyTable1");

            Assert.Equal(1, database.Tables.Count);
            Assert.Same(table0, database.Tables[0]);
        }
Exemplo n.º 12
0
        private static Index BuildIndex(DatabaseModel database, IIndex index)
        {
            Check.NotNull(index, "index");

            var table = database.GetTable(GetSchemaQualifiedName(index.EntityType));
            var columns = index.Properties.Select(
                p => table.GetColumn(p.ColumnName())).ToArray();

            var storeIndex = new Index(
                IndexName(index), columns, index.IsUnique, index.IsClustered());

            table.AddIndex(storeIndex);

            return storeIndex;
        }
Exemplo n.º 13
0
        private ForeignKey BuildForeignKey(DatabaseModel database, IForeignKey foreignKey)
        {
            Check.NotNull(foreignKey, "foreignKey");

            var table = database.GetTable(GetSchemaQualifiedName(foreignKey.EntityType));
            var referencedTable = database.GetTable(GetSchemaQualifiedName(foreignKey.ReferencedEntityType));
            var columns = foreignKey.Properties.Select(
                p => table.GetColumn(p.ColumnName())).ToArray();
            var referenceColumns = foreignKey.ReferencedProperties.Select(
                p => referencedTable.GetColumn(p.ColumnName())).ToArray();
            var cascadeDelete = foreignKey.CascadeDelete();

            var storeForeignKey = new ForeignKey(
                ForeignKeyName(foreignKey), columns, referenceColumns, cascadeDelete);

            table.AddForeignKey(storeForeignKey);

            return storeForeignKey;
        }
Exemplo n.º 14
0
        private PrimaryKey BuildPrimaryKey(DatabaseModel database, IKey primaryKey)
        {
            Check.NotNull(primaryKey, "primaryKey");

            var table = database.GetTable(GetSchemaQualifiedName(primaryKey.EntityType));
            var columns = primaryKey.Properties.Select(
                p => table.GetColumn(p.ColumnName())).ToArray();
            var isClustered = primaryKey.IsClustered();

            table.PrimaryKey = new PrimaryKey(PrimaryKeyName(primaryKey), columns, isClustered);

            return table.PrimaryKey;
        }
Exemplo n.º 15
0
        private static Table BuildTable(DatabaseModel database, IEntityType entityType)
        {
            var table = new Table(GetSchemaQualifiedName(entityType));

            database.AddTable(table);

            return table;
        }
 MigrationOperationSqlGenerator IMigrationOperationSqlGeneratorFactory.Create(DatabaseModel database)
 {
     return Create(database);
 }
Exemplo n.º 17
0
        public void AddTable_adds_specified_table()
        {
            var database = new DatabaseModel();

            Assert.Equal(0, database.Tables.Count);

            var table = new Table("dbo.MyTable");
            database.AddTable(table);

            Assert.Equal(1, database.Tables.Count);
            Assert.Same(table, database.Tables[0]);
        }
Exemplo n.º 18
0
        public void Create_and_initialize_database()
        {
            var database = new DatabaseModel();

            Assert.NotNull(database.Tables);
        }