public void TestNaturalKey()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);

            var diagrams = schema.AddTable("Diagrams")
                .AddColumn("diagram_id", DbType.Int32).AddPrimaryKey()
                .AddColumn<string>("name").AddLength(10).AddUniqueKey()
                .AddColumn<int>("principal_id")
                .Table;
            diagrams.UniqueKeys.Single().AddColumn(diagrams.FindColumn("principal_id"));

            DatabaseSchemaFixer.UpdateReferences(schema);

            var settings = new CodeWriterSettings { Namespace = "MyTest", CodeTarget = CodeTarget.PocoNHibernateHbm };
            PrepareSchemaNames.Prepare(schema, settings.Namer);

            //act
            var target = new MappingWriter(diagrams, settings);
            var txt = target.Write();

            //assert
            var errors = Validate(txt);
            Assert.IsFalse(errors);
        }
        public void TestCompositeKey()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);

            var orderDetail = schema.AddTable("OrderDetails")
                .AddColumn("OrderID", DbType.Int32).AddPrimaryKey()
                .AddColumn("ProductID", DbType.Int32)
                .AddColumn<int>("UnitPrice")
                .Table;
            orderDetail.PrimaryKey.AddColumn(orderDetail.FindColumn("ProductID"));

            DatabaseSchemaFixer.UpdateReferences(schema);

            var settings = new CodeWriterSettings { Namespace = "MyTest", CodeTarget = CodeTarget.PocoNHibernateHbm };
            PrepareSchemaNames.Prepare(schema, settings.Namer);

            //act
            var target = new MappingWriter(orderDetail, settings);
            var txt = target.Write();

            //assert
            var errors = Validate(txt);
            Assert.IsFalse(errors);
        }
        public void TestSimpleMapping()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);

            schema.AddTable("Categories")
                .AddColumn("CategoryId", DbType.Int32).AddPrimaryKey()
                .AddColumn("CategoryName", DbType.String);

            var products = schema.AddTable("Products")
                .AddColumn("ProductId", DbType.Int32).AddPrimaryKey()
                .AddColumn("ProductName", DbType.String)
                .AddColumn("CategoryId", DbType.Int32).AddForeignKey("fk", "Categories")
                .Table;

            DatabaseSchemaFixer.UpdateReferences(schema);

            var settings = new CodeWriterSettings { Namespace = "MyTest", CodeTarget = CodeTarget.PocoNHibernateHbm };
            PrepareSchemaNames.Prepare(schema, settings.Namer);

            //act
            var target = new MappingWriter(products, settings);
            var txt = target.Write();

            //assert
            var errors = Validate(txt);
            Assert.IsFalse(errors);
        }
示例#4
0
        private void WriteMapping(DatabaseTable table, ProjectWriter pw)
        {
            string fileName;
            switch (_codeWriterSettings.CodeTarget)
            {
                case CodeTarget.PocoNHibernateFluent:
                    fileName = WriteFluentMapping(table);
                    pw.AddClass(@"Mapping\" + fileName);
                    break;
                case CodeTarget.PocoNHibernateHbm:
                    //TPT subclasses are mapped in base class
                    if (table.FindInheritanceTable() != null) return;
                    var mw = new MappingWriter(table, _codeWriterSettings);
                    var txt = mw.Write();

                    fileName = table.NetName + ".hbm.xml";
                    var path = Path.Combine(_mappingPath, fileName);
                    File.WriteAllText(path, txt);
                    pw.AddMap(@"mapping\" + fileName);
                    break;
                case CodeTarget.PocoEntityCodeFirst:
                case CodeTarget.PocoRiaServices:
                    case CodeTarget.PocoEfCore:
                    var cfmw = new CodeFirstMappingWriter(table, _codeWriterSettings, _mappingNamer);
                    var cfmap = cfmw.Write();

                    fileName = cfmw.MappingClassName + ".cs";

                    var filePath = Path.Combine(_mappingPath, fileName);
                    File.WriteAllText(filePath, cfmap);
                    pw.AddClass(@"Mapping\" + fileName);
                    break;
            }
        }
        public void WriteNHibernateMappingInverseForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var table = schema.FindTableByName("Orders");

            var cw = new MappingWriter(table, new CodeWriterSettings());

            //act
            var txt = cw.Write();

            //assert
            var hasBillingAddress = txt.Contains("<many-to-one name=\"BillingAddress\" class=\"Address\" />");
            var hasDeliveryAddress = txt.Contains("<many-to-one name=\"DeliveryAddress\" class=\"Address\" />");

            Assert.IsTrue(hasBillingAddress);
            Assert.IsTrue(hasDeliveryAddress);
        }
        public void WriteNHibernateMappingForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var table = schema.FindTableByName("Address");

            var cw = new MappingWriter(table, new CodeWriterSettings());

            //act
            var txt = cw.Write();

            //assert
            var hasBillingAddress = txt.Contains("<bag name=\"BillingAddressCollection\" table=\"`Orders`\"");
            var hasDeliveryAddress = txt.Contains("<bag name=\"DeliveryAddressCollection\" table=\"`Orders`\"");

            Assert.IsTrue(hasBillingAddress);
            Assert.IsTrue(hasDeliveryAddress);
        }