Ejemplo n.º 1
0
        public void MultipleCallsResetsExistingAsms()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");
            classBuilder.CreateModelTypes("reqTbl");
        }
Ejemplo n.º 2
0
        public void RequiredColumnAttrIfColumnIsNullableIsTrue()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property = classBuilder.ExistingAssemblies.First().Value.GetProperty("reqCol");

            Assert.IsNotNull(property.GetCustomAttribute <RequiredAttribute>());
        }
Ejemplo n.º 3
0
        public void ComputedColumnAttribute()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property = classBuilder.ExistingAssemblies.First().Value.GetProperty("compCol");

            Assert.AreEqual(DatabaseGeneratedOption.Computed, property.GetCustomAttribute <DatabaseGeneratedAttribute>().DatabaseGeneratedOption);
        }
Ejemplo n.º 4
0
        public void PkColumnWithIdentity()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property = classBuilder.ExistingAssemblies.First().Value.GetProperty("pkColIdentity");

            Assert.IsNotNull(property.GetCustomAttribute <CompoundPrimaryKeyAttribute>());
            Assert.AreEqual(DatabaseGeneratedOption.Identity, property.GetCustomAttribute <DatabaseGeneratedAttribute>().DatabaseGeneratedOption);
        }
Ejemplo n.º 5
0
        public void FkColumnCompoundKey()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property = classBuilder.ExistingAssemblies.First().Value.GetProperty("fkCol31");

            Assert.AreEqual("Tbl12ObjectFromFkCol31AndFkCol32", property.GetCustomAttribute <ForeignKeyAttribute>().Name);
            Assert.AreEqual("db3", property.GetCustomAttribute <ReferencedDbObjectAttribute>().DbName);
            Assert.AreEqual("sch2", property.GetCustomAttribute <ReferencedDbObjectAttribute>().SchemaName);
            Assert.AreEqual("tbl12", property.GetCustomAttribute <ReferencedDbObjectAttribute>().TableName);
        }
Ejemplo n.º 6
0
        public void TableLevelProperties()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var type = classBuilder.ExistingAssemblies.First().Value;

            Assert.IsFalse(type.GetCustomAttribute <DataContractAttribute>().IsReference);
            Assert.AreEqual("tbl", type.GetCustomAttribute <TableAttribute>().Name);
            Assert.AreEqual("sch1", type.GetCustomAttribute <TableAttribute>().Schema);
            Assert.AreEqual("db1", type.GetCustomAttribute <DatabaseNameAttribute>().DatabaseName);
        }
Ejemplo n.º 7
0
        public void IntColumnsProperties()
        {
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property = classBuilder.ExistingAssemblies.First().Value.GetProperty("intCol");

            Assert.AreEqual("Int Col", property.GetCustomAttribute <DisplayNameAttribute>().DisplayName);
            Assert.AreEqual("intCol", property.GetCustomAttribute <ColumnAttribute>().Name);
            Assert.AreEqual(null, property.GetCustomAttribute <ColumnAttribute>().TypeName);
            Assert.IsNotNull(property.GetCustomAttribute <DataMemberAttribute>());
            Assert.IsTrue(property.GetCustomAttribute <BrowsableAttribute>().Browsable);
        }
Ejemplo n.º 8
0
        public void RebuildAllTypesIfInTheMiddleOfCreatingType()
        {
            //Create so already found
            string asmName  = $"dynamicassembly.tbl_{guid1}123456.reqTbl987654";
            string typename = asmName.Replace("dynamicassembly", "DynamicType");

            CreateAsm(asmName, $"tbl_{guid1}", createType: false);

            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            Assert.AreEqual(1, classBuilder.ExistingAssemblies.Count());
            Assert.AreEqual($"TBL_{guid1.ToString().ToUpper()}", classBuilder.ExistingAssemblies[asmName.ToUpperInvariant()].Name);
            Assert.AreEqual(2, AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.ToUpperInvariant() == asmName.ToUpperInvariant()).Count());
        }
Ejemplo n.º 9
0
        public virtual void SetupDataContext(string tableName)
        {
            var table = _dbSchema.Tables.Find(x => x.VariableName == tableName) ?? _dbSchema.Views.Find(x => x.VariableName == tableName);

            _dynamicClassBuilder.CreateModelTypes(tableName);

            var dbContextOptions = _dbSchemaBuilder.CreateDbContextOptions();

            Instance = (DbContextBase)Activator.CreateInstance(typeof(DbContextBase),
                                                               System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance,
                                                               null,
                                                               new object[] { dbContextOptions, _dynamicClassBuilder.ExistingAssemblies, _dbSchema.Tables },
                                                               null);

            //Entity DbSet
            var assemblyName = Utils.AssemblyName(table, tableName, _dbSchema.GetHashCode());

            DbSetEntityType = _dynamicClassBuilder.ExistingAssemblies.Where(x => x.Key.StartsWith(assemblyName, StringComparison.InvariantCultureIgnoreCase)).Single().Value;
        }
Ejemplo n.º 10
0
        public void RebuildAllTypesIfOnlySomeTimesFound()
        {
            var variableName = $"tbl12_{guid1}";
            var table        = new Mock <Table>()
            {
                CallBase = true
            };

            table.Setup(c => c.VariableName).Returns(variableName);
            table.Setup(c => c.GetHashCode()).Returns(1234566);

            var table2 = new Mock <Table>()
            {
                CallBase = true
            };

            table2.Setup(c => c.VariableName).Returns($"tbl2_{guid2}");
            table2.Setup(c => c.GetHashCode()).Returns(4561233);

            schema.Setup(c => c.Tables).Returns(new List <Table> {
                table.Object, table2.Object
            });

            //Create so already found
            string asmName = $"dynamicassembly.tbl12_{guid1}1234566.reqTbl987654";

            CreateAsm(asmName, $"tbl12_{guid1}");

            Assert.AreEqual(1, AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.ToUpperInvariant() == asmName.ToUpperInvariant()).Count());
            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Count());
            //all upper means generated
            Assert.AreEqual($"TBL12_{guid1.ToString().ToUpper()}", classBuilder.ExistingAssemblies[asmName.ToUpperInvariant()].Name);
            Assert.AreEqual(2, AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.ToUpperInvariant() == asmName.ToUpperInvariant()).Count());
        }
Ejemplo n.º 11
0
        public void Add1ToManyRelationship()
        {
            var table = new Mock <Table>()
            {
                CallBase = true
            };

            table.Object.Database = "db";
            table.Object.Schema   = "sch";
            table.Object.Name     = $"tbl1_{guid1}";
            table.Setup(c => c.Columns).Returns(new List <Column>
            {
                new Column {
                    ColumnName = "pkCol", PKName = "pk1", TableName = $"tbl1_{guid1}", ColumnType = "int", DataType = new DataType {
                        SystemType = typeof(int)
                    }
                },
            });
            table.Setup(c => c.GetHashCode()).Returns(123456);

            var table2 = new Mock <Table>()
            {
                CallBase = true
            };

            table2.Object.Database = "db";
            table2.Object.Schema   = "sch";
            table2.Object.Name     = $"tbl2_{guid2}";
            table2.Setup(c => c.Columns).Returns(new List <Column>
            {
                new Column {
                    ColumnName = "pkCol2", FKName = "fk1", TableName = $"tbl2_{guid2}", ReferencedColumn = "pkCol", ReferencedTable = $"tbl1_{guid1}", ReferencedSchema = "sch", ReferencedDatabase = "db", ColumnType = "int", DataType = new DataType {
                        SystemType = typeof(int)
                    }
                },
            });
            table2.Setup(c => c.GetHashCode()).Returns(456123);

            schema.Setup(c => c.Tables).Returns(new List <Table> {
                table.Object, table2.Object
            });

            var classBuilder = new DynamicClassBuilder(schema.Object);

            classBuilder.CreateModelTypes("reqTbl");

            var property    = classBuilder.ExistingAssemblies[$"DYNAMICASSEMBLY.DB_SCH_TBL1_{guid1.ToString().ToUpper()}123456.REQTBL987654"].GetProperty($"Tbl2_{guid2}ListFromPkCol2");
            var refProperty = classBuilder.ExistingAssemblies[$"DYNAMICASSEMBLY.DB_SCH_TBL2_{guid2.ToString().ToUpper()}456123.REQTBL987654"].GetProperty($"Tbl1_{guid1}ObjectFromPkCol2");


            Assert.AreEqual($"Tbl1_{guid1}ObjectFromPkCol2", property.GetCustomAttribute <InversePropertyAttribute>().Property);
            Assert.AreEqual($"Tbl2 {guid2} List From Pk Col2".ToUpper(), property.GetCustomAttribute <DisplayNameAttribute>().DisplayName.ToUpper());
            Assert.IsNotNull(property.GetCustomAttribute <DataMemberAttribute>());
            Assert.IsTrue(property.GetCustomAttribute <BrowsableAttribute>().Browsable);
            Assert.AreEqual("db", property.GetCustomAttribute <ReferencedDbObjectAttribute>().DbName);
            Assert.AreEqual("sch", property.GetCustomAttribute <ReferencedDbObjectAttribute>().SchemaName);
            Assert.AreEqual($"tbl2_{guid2}", property.GetCustomAttribute <ReferencedDbObjectAttribute>().TableName);

            Assert.AreEqual($"Tbl1 {guid1} Object From Pk Col2".ToUpper(), refProperty.GetCustomAttribute <DisplayNameAttribute>().DisplayName.ToUpper());
            Assert.IsNotNull(refProperty.GetCustomAttribute <DataMemberAttribute>());
            Assert.IsTrue(refProperty.GetCustomAttribute <BrowsableAttribute>().Browsable);
            Assert.AreEqual("db", refProperty.GetCustomAttribute <ReferencedDbObjectAttribute>().DbName);
            Assert.AreEqual("sch", refProperty.GetCustomAttribute <ReferencedDbObjectAttribute>().SchemaName);
            Assert.AreEqual($"tbl1_{guid1}", refProperty.GetCustomAttribute <ReferencedDbObjectAttribute>().TableName);
        }
Ejemplo n.º 12
0
        public void AddForigenKeyUpdatesForcesRegen()
        {
            Table tbl1 = new Table
            {
                Name     = "tbl1" + tableSuffix,
                Schema   = "sch1",
                Database = "db1",
            };

            tbl1.Columns.AddRange(
                new List <Column>
            {
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl1" + tableSuffix,
                    ColumnName  = "Id1",
                    ColumnType  = "int",
                    PKName      = "pk",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                },
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl1" + tableSuffix,
                    ColumnName  = "fkCol",
                    ColumnType  = "int",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                }
            });

            Table tbl2 = new Table
            {
                Name     = "tbl2" + tableSuffix,
                Schema   = "sch1",
                Database = "db1"
            };

            tbl2.Columns.AddRange(
                new List <Column>
            {
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl2" + tableSuffix,
                    ColumnName  = "Id2",
                    ColumnType  = "int",
                    PKName      = "pk",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                },
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl2" + tableSuffix,
                    ColumnName  = "name",
                    ColumnType  = "varchar",
                    DataType    = new DataType
                    {
                        SystemType = typeof(string)
                    }
                }
            });

            var initList = new List <Table> {
                tbl1, tbl2
            };

            var dbSchema = new Mock <IDbSchema>();

            dbSchema.SetupGet(c => c.Tables).Returns(initList);
            dbSchema.SetupGet(c => c.Views).Returns(new List <View>());

            DynamicClassBuilder classBuilder = new DynamicClassBuilder(dbSchema.Object);

            classBuilder.CreateModelTypes("tbl1" + tableSuffix);

            //Created types and total types are the same
            Assert.AreEqual(2, GetPrivateTypeBuilderCount(classBuilder));
            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Count);

            //Properties are just the columns
            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Values.First().GetProperties().Length);
            Assert.AreEqual(2, classBuilder.ExistingAssemblies.Values.Last().GetProperties().Length);

            //Keep same columns but link to table 1
            tbl2.Columns.Clear();
            tbl2.Columns.AddRange(new List <Column>
            {
                new Column
                {
                    TableSchema = "sch1",
                    TableName   = "tbl2" + tableSuffix,
                    ColumnName  = "Id2",
                    ColumnType  = "int",
                    PKName      = "pk",
                    DataType    = new DataType
                    {
                        SystemType = typeof(int)
                    }
                },
                new Column
                {
                    TableSchema        = "sch1",
                    TableName          = "tbl2" + tableSuffix,
                    ColumnName         = "name",
                    ColumnType         = "varchar",
                    FKName             = "fk",
                    ReferencedDatabase = "db1",
                    ReferencedSchema   = "sch1",
                    ReferencedTable    = "tbl1" + tableSuffix,
                    ReferencedColumn   = "Id2",
                    DataType           = new DataType
                    {
                        SystemType = typeof(string)
                    }
                }
            });



            var dbSchema2 = new Mock <IDbSchema>();

            dbSchema2.SetupGet(c => c.Tables).Returns(initList);
            dbSchema2.SetupGet(c => c.Views).Returns(new List <View>());

            DynamicClassBuilder classBuilder2 = new DynamicClassBuilder(dbSchema2.Object);

            classBuilder2.CreateModelTypes("tbl1" + tableSuffix);

            //Created types and total types are the same
            Assert.AreEqual(2, GetPrivateTypeBuilderCount(classBuilder2), "Not generating asms.");
            Assert.AreEqual(2, classBuilder2.ExistingAssemblies.Count);

            //Properties are include the columns and navproperties
            Assert.AreEqual(3, classBuilder2.ExistingAssemblies.Values.First().GetProperties().Length);
            Assert.AreEqual(3, classBuilder2.ExistingAssemblies.Values.Last().GetProperties().Length);
        }