コード例 #1
0
        public void WriteTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);
            var table = schema.AddTable("Categories")
                .AddColumn("CategoryId", "INT").AddPrimaryKey().AddIdentity()
                .AddColumn("CategoryName", "NVARCHAR").Table;
            //we need datatypes
            schema.DataTypes.Add(new DataType("INT", "System.Int32"));
            schema.DataTypes.Add(new DataType("NVARCHAR", "System.String"));
            DatabaseSchemaFixer.UpdateDataTypes(schema);
            //make sure .Net names are assigned
            PrepareSchemaNames.Prepare(schema, new Namer());

            //inject the custom code inserter
            var codeWriterSettings = new CodeWriterSettings {CodeInserter = new CustomCodeInserter()};
            var cw = new ClassWriter(table, codeWriterSettings);

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

            //assert
            Assert.IsTrue(txt.Contains("using System.ComponentModel.DataAnnotations.Schema"));
            Assert.IsTrue(txt.Contains("[Table(\"Categories\")]"));
            Assert.IsTrue(txt.Contains("[Column(\"CategoryId\")]"));
        }
コード例 #2
0
        public void WriteTest()
        {
            //arrange
            var schema = new DatabaseSchema(null, null);
            var table = schema.AddTable("Categories")
                .AddColumn("CategoryId", "INT").AddPrimaryKey().AddIdentity()
                .AddColumn("CategoryName", "NVARCHAR").Table;
            //we need datatypes
            schema.DataTypes.Add(new DataType("INT", "System.Int32"));
            schema.DataTypes.Add(new DataType("NVARCHAR", "System.String"));
            DatabaseSchemaFixer.UpdateDataTypes(schema);
            //make sure .Net names are assigned
            PrepareSchemaNames.Prepare(schema, new Namer());

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

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

            //assert
            var hasName = txt.Contains("public class Category");
            var hasCategoryId = txt.Contains("public virtual int CategoryId { get; set; }");
            var hasCategoryName = txt.Contains("public virtual string CategoryName { get; set; }");

            Assert.IsTrue(hasName);
            Assert.IsTrue(hasCategoryId);
            Assert.IsTrue(hasCategoryName);
        }
コード例 #3
0
        public void TestCompositeKeyWithCodeFirst()
        {
            //arrange
            var schema = Arrange();
            var table = schema.FindTableByName("Origin");
            var cw = new ClassWriter(table, new CodeWriterSettings { CodeTarget = CodeTarget.PocoEntityCodeFirst });

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

            //assert
            //non virtual actual properties
            var hasKey = txt.Contains("public string OriginKey1 { get; set; }");
            Assert.IsTrue(hasKey);
        }
コード例 #4
0
        public void TestCompositeKey()
        {
            //arrange
            var schema = Arrange();
            var table = schema.FindTableByName("Origin");
            var cw = new ClassWriter(table, new CodeWriterSettings());

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

            //assert
            //virtual of type tableName+Key named Key
            var hasKey = txt.Contains("public virtual OriginKey Key { get; set; }");
            Assert.IsTrue(hasKey);
        }
コード例 #5
0
        public void WriteForeignKeyInverseTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var categoryTable = schema.FindTableByName("Categories");

            var cw = new ClassWriter(categoryTable, new CodeWriterSettings());

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

            //assert
            var hasProducts = txt.Contains("public virtual IList<Product> ProductCollection { get; private set; }");

            Assert.IsTrue(hasProducts);
        }
コード例 #6
0
        public void TestTablePerTypeSubClass()
        {
            //arrange
            var schema = Arrange();
            //var schema = Arrange();
            var table = schema.FindTableByName("Cars");
            var codeWriterSettings = new CodeWriterSettings { CodeTarget = CodeTarget.PocoEntityCodeFirst };

            var classWriter = new ClassWriter(table, codeWriterSettings);

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

            //assert
            Assert.IsTrue(txt.Contains("public class Car : Vehicle"), "Subclass inherits from parent class");
            Assert.IsFalse(txt.Contains("public int VehicleId { get; set; }"), "The primary key is marked on the parent class");
        }
コード例 #7
0
        public void WriteMultipleForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var table = schema.FindTableByName("Orders");

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

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

            //assert
            var hasBillingAddress = txt.Contains("public virtual Address BillingAddress { get; set; }");
            var hasDeliveryAddress = txt.Contains("public virtual Address DeliveryAddress { get; set; }");

            Assert.IsTrue(hasBillingAddress);
            Assert.IsTrue(hasDeliveryAddress);
        }
コード例 #8
0
        public void WriteInverseForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var table = schema.FindTableByName("Address");

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

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

            //assert
            var hasBillingAddress = txt.Contains("public virtual IList<Order> BillingAddressCollection { get; private set; }");
            var hasDeliveryAddress = txt.Contains("public virtual IList<Order> DeliveryAddressCollection { get; private set; }");

            Assert.IsTrue(hasBillingAddress);
            Assert.IsTrue(hasDeliveryAddress);
        }
コード例 #9
0
        public void WriteForeignKeyInverseTestForNHibernate()
        {
            //arrange
            var schema = ArrangeSchema();
            var categoryTable = schema.FindTableByName("Categories");

            var codeWriterSettings = new CodeWriterSettings();
            codeWriterSettings.CodeTarget = CodeTarget.PocoNHibernateHbm;
            var cw = new ClassWriter(categoryTable, codeWriterSettings);

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

            //assert
            var hasProducts = txt.Contains("public virtual IList<Product> ProductCollection { get; protected set; }");

            Assert.IsTrue(hasProducts, "NHibernate 3.2 requires *all* setters to be protected or public");
        }
コード例 #10
0
        public void WriteCodeFirstTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var categoryTable = schema.FindTableByName("Categories");

            var cw = new ClassWriter(categoryTable, new CodeWriterSettings { CodeTarget = CodeTarget.PocoEntityCodeFirst });

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

            //assert
            var hasCategoryId = txt.Contains("public int CategoryId { get; set; }");
            var hasProducts = txt.Contains("public virtual ICollection<Product> ProductCollection { get; private set; }");

            Assert.IsTrue(hasCategoryId, "Ordinary scalar properties don't need to be virtual");
            Assert.IsTrue(hasProducts);
        }
コード例 #11
0
        public void TestAssociationTable()
        {
            //arrange
            var schema = Arrange();
            var table = schema.FindTableByName("ProductCategories");
            var cw = new ClassWriter(table, new CodeWriterSettings { CodeTarget = CodeTarget.PocoEntityCodeFirst });

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

            //assert
            var hasCategoryId = txt.Contains("public int CategoryId { get; set; }");
            var hasCategory = txt.Contains("public virtual Category Category { get; set; }");
            var hasProductId = txt.Contains("public int ProductId { get; set; }");
            var hasProduct = txt.Contains("public virtual Product Product { get; set; }");
            Assert.IsTrue(hasCategoryId);
            Assert.IsTrue(hasCategory);
            Assert.IsTrue(hasProductId);
            Assert.IsTrue(hasProduct);
        }
コード例 #12
0
        public void TestCompositeForeignKeyCodeFirst()
        {
            //arrange
            var schema = Arrange();
            var table = schema.FindTableByName("Products");
            var cw = new ClassWriter(table, new CodeWriterSettings
            {
                CodeTarget = CodeTarget.PocoEntityCodeFirst,
                UseForeignKeyIdProperties = true
            });

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

            //assert
            //virtual of type tableName+Key named Key
            var hasKeyId1 = txt.Contains("public string OriginKey1 { get; set; }");
            var hasKeyId2 = txt.Contains("public string OriginKey2 { get; set; }");
            var hasForeignKey = txt.Contains("public virtual Origin Origin { get; set; }");
            Assert.IsTrue(hasKeyId1);
            Assert.IsTrue(hasKeyId2);
            Assert.IsTrue(hasForeignKey);
        }
コード例 #13
0
        /// <summary>
        /// Uses the specified schema to write class files, NHibernate/EF CodeFirst mapping and a project file. Any existing files are overwritten. If not required, simply discard the mapping and project file. Use these classes as ViewModels in combination with the data access strategy of your choice.
        /// </summary>
        /// <param name="directory">The directory to write the files to. Will create a subdirectory called "mapping". The directory must exist- any files there will be overwritten.</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="InvalidOperationException"/>
        /// <exception cref="IOException"/>
        /// <exception cref="UnauthorizedAccessException"/>
        /// <exception cref="System.Security.SecurityException" />
        public void Execute(DirectoryInfo directory)
        {
            if (directory == null)
                throw new ArgumentNullException("directory");
            if (!directory.Exists)
                throw new InvalidOperationException("Directory does not exist: " + directory.FullName);

            var pw = CreateProjectWriter();

            InitMappingProjects(directory, pw);
            _mappingNamer = new MappingNamer();

            foreach (var table in _schema.Tables)
            {
                if (FilterIneligible(table)) continue;
                var className = table.NetName;
                UpdateEntityNames(className, table.Name);

                var cw = new ClassWriter(table, _codeWriterSettings);
                var txt = cw.Write();

                var fileName = WriteClassFile(directory, className, txt);
                pw.AddClass(fileName);

                WriteMapping(table, pw);
            }

            if (_codeWriterSettings.IncludeViews)
            {
                foreach (var view in _schema.Views)
                {
                    var className = view.NetName;
                    UpdateEntityNames(className, view.Name);

                    var cw = new ClassWriter(view, _codeWriterSettings);
                    var txt = cw.Write();

                    var fileName = WriteClassFile(directory, className, txt);
                    pw.AddClass(fileName);

                    WriteMapping(view, pw);
                }
            }

            string contextName = null;
            if (IsCodeFirst())
            {
                contextName = WriteDbContext(directory, pw);
            }

            //we could write functions (at least scalar functions- not table value functions)
            //you have to check the ReturnType (and remove it from the arguments collections).
            if (_codeWriterSettings.WriteStoredProcedures)
            {
                WriteStoredProcedures(directory.FullName, pw);
                WritePackages(directory.FullName, pw);
            }
            if (_codeWriterSettings.WriteUnitTest)
                WriteUnitTest(directory.FullName, contextName);

            WriteProjectFile(directory, pw);
        }
コード例 #14
0
        /// <summary>
        /// Uses the specified schema to write class files, NHibernate/EF CodeFirst mapping and a project file. Any existing files are overwritten. If not required, simply discard the mapping and project file. Use these classes as ViewModels in combination with the data access strategy of your choice.
        /// </summary>
        /// <param name="directory">The directory to write the files to. Will create a subdirectory called "mapping". The directory must exist- any files there will be overwritten.</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="InvalidOperationException"/>
        /// <exception cref="IOException"/>
        /// <exception cref="UnauthorizedAccessException"/>
        /// <exception cref="System.Security.SecurityException" />
        public void Execute(DirectoryInfo directory)
        {
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }
            if (!directory.Exists)
            {
                throw new InvalidOperationException("Directory does not exist: " + directory.FullName);
            }

            var pw = CreateProjectWriter();

            InitMappingProjects(directory, pw);
            _mappingNamer = new MappingNamer();

            foreach (var table in _schema.Tables)
            {
                if (FilterIneligible(table))
                {
                    continue;
                }
                var className = table.NetName;
                UpdateEntityNames(className, table.Name);

                var cw  = new ClassWriter(table, _codeWriterSettings);
                var txt = cw.Write();

                var fileName = WriteClassFile(directory, className, txt);
                pw.AddClass(fileName);

                WriteMapping(table, pw);
            }

            if (_codeWriterSettings.IncludeViews)
            {
                foreach (var view in _schema.Views)
                {
                    var className = view.NetName;
                    UpdateEntityNames(className, view.Name);

                    var cw  = new ClassWriter(view, _codeWriterSettings);
                    var txt = cw.Write();

                    var fileName = WriteClassFile(directory, className, txt);
                    pw.AddClass(fileName);

                    WriteMapping(view, pw);
                }
            }


            string contextName = null;

            if (IsCodeFirst())
            {
                contextName = WriteDbContext(directory, pw);
            }

            //we could write functions (at least scalar functions- not table value functions)
            //you have to check the ReturnType (and remove it from the arguments collections).
            if (_codeWriterSettings.WriteStoredProcedures)
            {
                WriteStoredProcedures(directory.FullName, pw);
                WritePackages(directory.FullName, pw);
            }
            if (_codeWriterSettings.WriteUnitTest)
            {
                WriteUnitTest(directory.FullName, contextName);
            }

            WriteProjectFile(directory, pw);
        }
コード例 #15
0
        public void WriteForeignKeyTest()
        {
            //arrange
            var schema = ArrangeSchema();
            var productsTable = schema.FindTableByName("Products");

            var cw = new ClassWriter(productsTable, new CodeWriterSettings());

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

            //assert
            var hasCategory = txt.Contains("public virtual Category Category { get; set; }");

            Assert.IsTrue(hasCategory);
        }
コード例 #16
0
        public void WriteSharedPrimaryKeyTest()
        {
            //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 cw = new ClassWriter(table, new CodeWriterSettings { CodeTarget = CodeTarget.PocoEntityCodeFirst });

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

            //assert
            var hasScalarKey = txt.Contains("public string Regnum { get; set; }");
            var hasForeignKey = txt.Contains("public virtual Vehicle Vehicle { get; set; }");

            Assert.IsTrue(hasScalarKey);
            Assert.IsTrue(hasForeignKey);
        }
コード例 #17
0
ファイル: SqlTasks.cs プロジェクト: Petran15/dbschemareader
 public void BuildClass(DatabaseTable databaseTable)
 {
     try
     {
         var cw = new ClassWriter(databaseTable, new CodeWriterSettings());
         var txt = cw.Write();
         Clipboard.SetText(txt, TextDataFormat.UnicodeText);
     }
     catch (Exception exception)
     {
         Debug.WriteLine(exception.Message);
     }
 }
コード例 #18
0
        public void WriteViewTest()
        {
            //arrange
            var view = new DatabaseView();
            view.Name = "AlphabeticNames";
            view.AddColumn("FirstName", typeof(string)).AddNullable()
                .AddColumn("LastName", typeof(string)).AddNullable();

            var schema = new DatabaseSchema(null, null);
            schema.Views.Add(view);
            PrepareSchemaNames.Prepare(schema, new Namer());

            var codeWriterSettings = new CodeWriterSettings
            {
                CodeTarget = CodeTarget.PocoNHibernateHbm,
                IncludeViews = true
            };
            var cw = new ClassWriter(view, codeWriterSettings);

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

            //assert
            var hasFirstName = txt.Contains("public virtual string FirstName");
            var hasLastName = txt.Contains("public virtual string LastName");
            var hasEquals = txt.Contains("public override bool Equals(object obj)");

            Assert.IsTrue(hasFirstName);
            Assert.IsTrue(hasLastName);
            Assert.IsTrue(hasEquals);
        }