public void JoinTableCreated()
        {
            //arrange
            var products           = _schema.FindTableByName("CategoryProducts");
            var codeWriterSettings = new CodeWriterSettings {
                CodeTarget = CodeTarget.PocoEfCore
            };
            var target = new CodeFirstMappingWriter(products, codeWriterSettings, new MappingNamer());

            //act
            var result = target.Write();

            //assert
            //        public static void Map(EntityTypeBuilder<CategoryProduct> b)
            //        {
            //            //table
            //            b.ToTable("CategoryProducts");
            //            // Primary key (composite)
            //            b.HasKey(x => new { x.CategoryId, x.ProductId });
            //            // Properties
            //            b.HasOne(x => x.Category).WithOne();
            //            b.HasOne(x => x.Product).WithOne();


            var hasKey =
                result.Contains("b.HasKey(x => new { x.CategoryId, x.ProductId });");
            //we have to create a many to many table for this
            var hasCategory = result.Contains("b.HasOne(x => x.Category).WithOne(");
            var hasProduct  = result.Contains("b.HasOne(x => x.Product).WithOne(");

            Assert.IsTrue(hasKey);
            Assert.IsTrue(hasCategory);
            Assert.IsTrue(hasProduct);
        }
        public void MappingNameWithConflictsTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);
            var table  = schema.AddTable("Products")
                         .AddColumn("ProductId", DbType.Int32).AddPrimaryKey().AddIdentity()
                         .Table;

            table.NetName = "Product";
            //we need datatypes
            schema.DataTypes.Add(new DataType("INT", "System.Int32"));
            //make sure it's all tied up
            DatabaseSchemaFixer.UpdateReferences(schema);


            var mappingNamer = new MappingNamer();
            var target       = new CodeFirstMappingWriter(table, new CodeWriterSettings(), mappingNamer);

            //we also have a table called "ProductMapping"
            //so we can't call the mapping class for "Product" the same name
            mappingNamer.EntityNames.Add("ProductMapping");
            mappingNamer.EntityNames.Add("ProductMappingMap");

            //act
            target.Write();
            //now the name is assigned
            var className = target.MappingClassName;

            //assert
            Assert.AreEqual("ProductMappingMapMap", className, "Should not conflict with the existing names");
            Assert.AreEqual(3, mappingNamer.EntityNames.Count, "Should add the name to the list");
        }
        public void HasMaxLengthTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);
            var table  = schema.AddTable("Products")
                         .AddColumn("ProductId", DbType.Int32).AddPrimaryKey().AddIdentity()
                         .AddColumn("ProductName", DbType.String).AddLength(20)
                         .Table;

            //we need datatypes
            schema.DataTypes.Add(new DataType("INT", "System.Int32"));
            schema.DataTypes.Add(new DataType("NVARCHAR", "System.String"));
            //make sure it's all tied up
            DatabaseSchemaFixer.UpdateReferences(schema);

            var target = new CodeFirstMappingWriter(table, new CodeWriterSettings(), new MappingNamer());

            //act
            var result = target.Write();

            //assert
            var hasMaxLength = result.Contains("Property(x => x.ProductName).HasMaxLength(20)");

            Assert.IsTrue(hasMaxLength);
        }
        public void MappingSharedPrimaryKeyTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);

            schema
            .AddTable("vehicle")
            .AddColumn <string>("regnum").AddPrimaryKey().AddLength(25)
            .AddColumn <string>("model").AddLength(32)
            .AddTable("car")
            .AddColumn <string>("regnum").AddLength(25).AddPrimaryKey().AddForeignKey("fk", "vehicle")
            .AddColumn <int>("doors");
            //make sure it's all tied up
            DatabaseSchemaFixer.UpdateReferences(schema);
            //make sure .Net names are assigned
            PrepareSchemaNames.Prepare(schema, new Namer());
            var table = schema.FindTableByName("car");

            var mappingNamer       = new MappingNamer();
            var codeWriterSettings = new CodeWriterSettings {
                CodeTarget = CodeTarget.PocoEntityCodeFirst
            };
            var target = new CodeFirstMappingWriter(table, codeWriterSettings, mappingNamer);

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

            //assert
            var hasScalarKey  = txt.Contains("HasKey(x => x.Regnum);");
            var hasForeignKey = txt.Contains("HasRequired(x => x.Vehicle);");

            Assert.IsTrue(hasScalarKey);
            Assert.IsTrue(hasForeignKey);
        }
Ejemplo n.º 5
0
        public void ForeignKeyIdTest()
        {
            //arrange
            DatabaseSchema schema             = PrepareModel();
            var            products           = schema.FindTableByName("Products");
            var            codeWriterSettings = new CodeWriterSettings
            {
                CodeTarget = CodeTarget.PocoEntityCodeFirst,
                UseForeignKeyIdProperties = true
            };
            var target = new CodeFirstMappingWriter(products, codeWriterSettings, new MappingNamer());

            //act
            var result = target.Write();

            //assert
            var hasMappingClass         = result.Contains("public class ProductMapping : EntityTypeConfiguration<Product>");
            var hasForeignKeyIdProperty =
                result.Contains("Property(x => x.SupplierKeyId).HasColumnName(\"SupplierKey\")");
            var hasForeignKey = result.Contains("HasRequired(x => x.SupplierKey).WithMany(c => c.ProductCollection).HasForeignKey(c => c.SupplierKeyId);");

            Assert.IsTrue(hasMappingClass);
            Assert.IsTrue(hasForeignKeyIdProperty);
            Assert.IsTrue(hasForeignKey);
        }
        public void IsImageTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);
            var table  = schema.AddTable("Products")
                         .AddColumn("ProductId", DbType.Int32).AddPrimaryKey().AddIdentity()
                         .AddColumn("Picture", "image")
                         .Table;

            //we need datatypes
            schema.DataTypes.Add(new DataType("INT", "System.Int32"));
            schema.DataTypes.Add(new DataType("image", "System.Byte[]"));
            //make sure it's all tied up
            DatabaseSchemaFixer.UpdateReferences(schema);

            var target = new CodeFirstMappingWriter(table, new CodeWriterSettings(), new MappingNamer());

            //act
            var result = target.Write();

            //assert
            //EF CF will default to varbinary so we must specify
            var hasImageType = result.Contains("Property(x => x.Picture).HasColumnType(\"image\")");

            Assert.IsTrue(hasImageType);
        }
Ejemplo n.º 7
0
        public void MappingNameTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);
            var table  = schema.AddTable("Products")
                         .AddColumn("ProductId", DbType.Int32).AddPrimaryKey().AddIdentity()
                         .Table;

            table.NetName = "Product";
            //we need datatypes
            schema.DataTypes.Add(new DataType("INT", "System.Int32"));
            //make sure it's all tied up
            DatabaseSchemaFixer.UpdateReferences(schema);

            var codeWriterSettings = new CodeWriterSettings {
                CodeTarget = CodeTarget.PocoEntityCodeFirst
            };
            var target = new CodeFirstMappingWriter(table, codeWriterSettings, new MappingNamer());

            //act
            target.Write();
            //now the name is assigned
            var className = target.MappingClassName;

            //assert
            Assert.AreEqual("ProductMapping", className);
        }
Ejemplo n.º 8
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:
                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:
                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 TestOneToOneTable()
        {
            //arrange
            var schema             = Arrange();
            var table              = schema.FindTableByName("Products");
            var codeWriterSettings = new CodeWriterSettings {
                CodeTarget = CodeTarget.PocoEntityCodeFirst
            };
            var target = new CodeFirstMappingWriter(table, codeWriterSettings, new MappingNamer());

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

            //assert
            Assert.IsTrue(txt.Contains("HasOptional(x => x.ProductDetail);"));
        }
        public void TableLinksToJoinTable()
        {
            //arrange
            var products           = _schema.FindTableByName("Categories");
            var codeWriterSettings = new CodeWriterSettings {
                CodeTarget = CodeTarget.PocoEfCore
            };
            var target = new CodeFirstMappingWriter(products, codeWriterSettings, new MappingNamer());

            //act
            var result = target.Write();

            //assert
            var hasCategory = result.Contains("b.HasMany(x => x.CategoryProductCollection).WithOne(");

            Assert.IsTrue(hasCategory);
        }
Ejemplo n.º 11
0
        public void WriteCodeFirstMappingForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var table  = schema.FindTableByName("Address");

            var cw = new CodeFirstMappingWriter(table, new CodeWriterSettings(), new MappingNamer());

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

            //assert
            var hasBillingAddress  = txt.Contains("HasMany(x => x.BillingAddressCollection);");
            var hasDeliveryAddress = txt.Contains("HasMany(x => x.DeliveryAddressCollection);");

            Assert.IsTrue(hasBillingAddress);
            Assert.IsTrue(hasDeliveryAddress);
        }
Ejemplo n.º 12
0
        public void WriteCodeFirstMappingInverseForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var table  = schema.FindTableByName("Orders");

            var cw = new CodeFirstMappingWriter(table, new CodeWriterSettings(), new MappingNamer());

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

            //assert
            var hasBillingAddress  = txt.Contains("HasRequired(x => x.BillingAddress).WithMany(c => c.BillingAddressCollection).Map(m => m.MapKey(\"BillingAddress\"));");
            var hasDeliveryAddress = txt.Contains("HasRequired(x => x.DeliveryAddress).WithMany(c => c.DeliveryAddressCollection).Map(m => m.MapKey(\"DeliveryAddress\"));");

            Assert.IsTrue(hasBillingAddress);
            Assert.IsTrue(hasDeliveryAddress);
        }
        public void ExecuteTest()
        {
            //arrange
            DatabaseSchema schema   = PrepareModel();
            var            products = schema.FindTableByName("Products");
            var            target   = new CodeFirstMappingWriter(products, new CodeWriterSettings(), new MappingNamer());

            //act
            var result = target.Write();

            //assert
            var hasMappingClass = result.Contains("public class ProductMapping : EntityTypeConfiguration<Product>");
            //we don't have the UseForeignKeyIdProperties=true, so map back to the instance property
            var hasForeignKey =
                result.Contains("HasRequired(x => x.SupplierKey).WithMany(c => c.ProductCollection).Map(m => m.MapKey(\"SupplierKey\"))");
            var hasManyToMany = result.Contains("HasMany(x => x.CategoryCollection).WithMany(z => z.ProductCollection)");

            Assert.IsTrue(hasMappingClass);
            Assert.IsTrue(hasForeignKey);
            Assert.IsTrue(hasManyToMany);
        }
Ejemplo n.º 14
0
        public void ExecuteTest()
        {
            //arrange
            DatabaseSchema schema             = PrepareModel();
            var            products           = schema.FindTableByName("Products");
            var            codeWriterSettings = new CodeWriterSettings {
                CodeTarget = CodeTarget.PocoEfCore
            };
            var target = new CodeFirstMappingWriter(products, codeWriterSettings, new MappingNamer());

            //act
            var result = target.Write();

            //assert
            var hasMappingClass = result.Contains("public static class ProductMapping");
            //we don't have the UseForeignKeyIdProperties=true, so map back to the instance property

            //EF v6 code
            //            HasRequired(x => x.SupplierKey).WithMany(c => c.ProductCollection).Map(m => m.MapKey("SupplierKey"));
            //            // Navigation properties
            //            // Many to many foreign key to Categories
            //            HasMany(x => x.CategoryCollection).WithMany(z => z.ProductCollection)
            //            .Map(map =>
            //            {
            //                map.ToTable("CategoryProducts");
            //                map.MapLeftKey("ProductId");
            //                map.MapRightKey("CategoryId");
            //            }


            var hasForeignKey =
                result.Contains("b.HasOne(x => x.SupplierKey).WithMany(c => c.ProductCollection);");

            //we have to create a many to many table for this
            //var hasManyToMany = result.Contains("HasMany(x => x.CategoryCollection).WithMany(z => z.ProductCollection)");

            Assert.IsTrue(hasMappingClass);
            Assert.IsTrue(hasForeignKey);
            //Assert.IsTrue(hasManyToMany);
        }