Automatic Table Generation in any database by NHibernate ORM and CodeDom for code ...

:

Introduction

This article describes how to allow users to build database automatically from application via two technologies code generation (CodeDOM) and NHibernate (Fluent) which let us make backend regardless of type of database and without involving into complicated concepts.

First of all I want to mention why I have selected NHibernate or in a better words Fluent NHibernate to do this. I have counted some of most popular advantage and disadvantage of NHibernate.

NHibernate

NHibernate is an Object Relational Mapping ORM framework. NHibernate locates between database and business layer and provides powerful mapping.  It inherits from Hibernate from Java world. It uses mapping file or attributes besides properties.

NHibernate Advantages:

  1. Powerful mapping facilities.
  2. Execute multiple queries in one going to database instead of going to DB for each query.
  3. Lazy Loading works for NHibernate and it means that you just fetch your necessary data on memory instead of the whole of collection in memory and it reduces overhead from memory.
  4. Decoupling from database, it means you can use various database type instead of just using SQL such as Oracle and etc.
  5. Writing code for NHibernate make developers feel better in the aspect of readability and documentary.
  6.  Cashing and second level caching.
  7. Its session factory is thread safe object in NHibernate.

 

NHibernate Disadvantages:

  1. Learning and mastering in NHibernate takes time and it seems cumbersome to be professional.
  2. NHibernate needs complicated XML configuration per entities.
  3.  It takes long time when you initiate it for first time because of preparation operation in metadata is heavy.

How NHibernate works?

NHibernate provides an ORM as free XML configuration. It depends on session factory which creates session in order to bind database. You need a class to define your specific configuration which introduces connection string for particular database. After that whenever you call session you are able to connect to your db. You can either write a code as traditional sql query or Linq query by using NHibernate.Linq as your namespace. Totally session encapsulate unit of work pattern too. You need to produce XML files to work with NHibenate but I have used Fluent NHibernate to prevent any cumbersome during implementation.

 

 

CodeDOM 

By Fluent NHibernate you need to write two classes for per entity as your table, one is simple and another for mapping and introduce relation to another table. It is a bit difficult if you use so many tables in your project to write two classes for per entity. Therefore I decided to use Code Document Object Model, CodeDOM, as a generation tool for generating these classes and I get table name, field names and their data type for parent table from UI and for child ones I get its parent table in order to build foreign key.

As a simplest explanation about CodeDOM: please image you have to generate so many classes with same structures but some parts have different expression so in order to save your time and energy it is better to write one class instead of so many class and that class as a good template generates all of your classes.

Below namespaces enable you to use CodeDOM:

using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

By using CodeDOM you can either write source code for ASP.NET, XML web services client proxies and etc or compile. Its structure is such a tree graph which its root is codecompileunit. Then you should define your namespace and import them. Determine your class name and fields and properties and add them as members to class.

CodeDOM Advantages:

        1. CodeDOM let you create, compile and execute your source code for application at run time, without                      writing a lot of lines or when your parameter should be determined at run time.

        2. CodeDOM uses single model to generate source code, so any languages that support CodeDOM can work             with it.

        3. It is possible to translate VB to C# or inverse.

CodeDOM Disadvantages:

        1. There are limitation to define any expression by CodeDOM which you have to use Snippet classes.

        2. Snippet classes can not satisfy any expression so you should not use heavy function on that.

 

Using the code Step by Step

1. Creating Database 

You should create database as "GenerateDB" which is a fundamental requirement for connection string. I have used SQL Server 2008 but you can use any database because NHibernate is over from database type.

 

I have defined table "DataType" with two columns for that. ID and Name for data type of entities. You can assign your data type name to "Name" field such as int, string, and etc. Be careful that CodeDOM pays attention to your application language data type for instance I used C# so I used int and string which is a bit different in VB language. In VB we use "integer" instead of "int".

 

 

2. Create MVC Project with Fluent NHibernate + Microsoft.Build for CodeDOM +  AngularJS for Frontend

I have used Microsoft Visual Studio 2013, MVC ASP.Net for this scenario. I have applied Fluent NHibernate instead of NHibernate because its source code generation is easy by CodeDOM. I imported Microsoft.Build library for CodeDOM.

Eventually I used  AngularJS for frontend and UI section, you can have more information about angularjs implementation at http://www.codeproject.com/Articles/869433/AngularJS-MVC-Repository-Dispose, and about MVC ASP.NET implementation at http://technical.cosmicverse.info/article/index/3.

File -> New Project -> ASP.NET MVC 4 Project -> Select "Internet Application"

Select "Empty"

Install Fluent NHibernate:

Go to Solution -> Right Click on "References" -> Select "Manage Nuget Packages"

Search "FluentNHibernate" -> click on "Install" in order to add its references

Look at Solution -> References -> There are three new references 

1. FluentNHibernate

2. Iesi.Collections

3. NHibernate

 

In order to use CodeDOM you should add "Microsoft.Build" and "Microsoft.Build.Framework"

 

Install AngularJS:

Go to Solution -> Right Click on "References" -> Select "Manage Nuget Packages"

Search: "AngularJS" -> click on "Install"

 

3. Review on Scenario and Testing

In this scenario I have assigned two text box just for two fields, you can create more than one according to your requirement. I will improve this section by creating text boxes at run time in newer version of this article so please bookmark this article for newer upgrade.

 

I create a parent table and child table which have one to many relation to each other. In below picture parent table has no parent so the last combo box should be empty.

But child table has relationship with parent by its calling parent table at last combo box.

Look at solution and you will see new classes have been generated by CodeDOM inside "Entities" and "Mapping". For each entitiy there are two classes, one in "Entities" folder such as "Parent.cs" and another is in "Mapping" folder as "ChildMap.cs".

After generation look at your database too. There is no difference what is your database SQL, Oacle or etc. Your tables have been generated here too by FluentNHibernate.

 

 

 

In a nutshell:

You determine your table spesification on UI and then angular send info to controller and there by using "GenerateTable.cs" and "GenerateMap.cs" with GenerateDBController/Generate Action , your entitie classes will be created on the "Entities" and "Mapping" folders. These classes such as "Parent.cs" and "ParentMap.cs" by FluentNHibernate utilities "NHibernate.cs" will create tables inside database.

 

4. Using Code

Configuration Fluent NHibernate:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using MVCNHibernate.Entities;     //ProjectName.Entites
using MVCNHibernate.Controllers; //ProjectName.Controllers

namespace MVCNHibernate
{
    public class NHibernate
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                    InitializeSessionFactory();
                return _sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
              _sessionFactory = Fluently.Configure()
              .Database(MsSqlConfiguration.MsSql2008
                            .ConnectionString(
                                @"Server=.;initial catalog=GenerateDB;integrated security=True")
                            .ShowSql()
              )
              .Mappings(m =>
                        m.FluentMappings
                            .AddFromAssemblyOf<generatedbcontroller>())
              .ExposeConfiguration(cfg => new SchemaUpdate(cfg)
                                              .Execute(false, true))
              .BuildSessionFactory();
        }

        public static ISession OpenSession()
        {
           return SessionFactory.OpenSession();
        }
    }
}</generatedbcontroller>

GenerateTable.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Web.Mvc;
using System.Web;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;


namespace MVCNHibernate
{
    public class GenerateTable
    {
        CodeCompileUnit targetUnit;

        CodeTypeDeclaration targetClass;
               
        public GenerateTable(string tableName)
        {
            targetUnit = new CodeCompileUnit();

            //Path
            CodeNamespace samples = new CodeNamespace("MVCNHibernate.Entities");
            
            //Namespace
            samples.Imports.Add(new CodeNamespaceImport("System"));
            samples.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            samples.Imports.Add(new CodeNamespaceImport("System.Linq"));
            samples.Imports.Add(new CodeNamespaceImport("System.Text"));
            samples.Imports.Add(new CodeNamespaceImport("MVCNHibernate.Entities"));

            targetClass = new CodeTypeDeclaration(tableName);
            targetClass.IsClass = true;
            targetClass.TypeAttributes =
                TypeAttributes.Public;
            samples.Types.Add(targetClass);
            targetUnit.Namespaces.Add(samples);
        }

        public void AddFields(string fld1, string dt1, string fld2, string dt2)
        {
            CodeMemberField field1 = new CodeMemberField();
            field1.Attributes = MemberAttributes.Private;
            if (dt1=="int")
            {
                field1.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt1 == "string")
            {
                field1.Type = new CodeTypeReference(typeof(System.String));
            }
            
            field1.Name = "_"+fld1;
            targetClass.Members.Add(field1);

            CodeMemberProperty property1 = new CodeMemberProperty();
            property1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1)));
            property1.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1), new CodePropertySetValueReferenceExpression()));
            property1.Attributes = MemberAttributes.Public ;
            property1.Name = fld1;
            if (dt1 == "int")
            {
                property1.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt1 == "string")
            {
                property1.Type = new CodeTypeReference(typeof(System.String));
            }
           
            targetClass.Members.Add(property1);

            CodeMemberField field2 = new CodeMemberField();
            field2.Attributes = MemberAttributes.Private;
            if (dt2 == "int")
            {
                field2.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt2 == "string")
            {
                field2.Type = new CodeTypeReference(typeof(System.String));
            }
            field2.Name = "_" + fld2;
            targetClass.Members.Add(field2);

            CodeMemberProperty property2 = new CodeMemberProperty();
            property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2)));
            property2.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2), new CodePropertySetValueReferenceExpression()));
            property2.Attributes = MemberAttributes.Public ;
            property2.Name = fld2;
            if (dt2 == "int")
            {
                property2.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt2 == "string")
            {
                property2.Type = new CodeTypeReference(typeof(System.String));
            }
           
            targetClass.Members.Add(property2);
        }
        
        public void RelationalAddFields(string tableName,string fld1, string dt1, string fld2, string dt2, string parent)
        {
            CodeMemberField field1 = new CodeMemberField();
            field1.Attributes = MemberAttributes.Private;
            if (dt1 == "int")
            {
                field1.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt1 == "string")
            {
                field1.Type = new CodeTypeReference(typeof(System.String));
            }

            field1.Name = "_" + fld1;
            targetClass.Members.Add(field1);

            CodeMemberProperty property1 = new CodeMemberProperty();
            property1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1)));
            property1.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1), new CodePropertySetValueReferenceExpression()));
            property1.Attributes = MemberAttributes.Public;
            property1.Name = fld1;
            if (dt1 == "int")
            {
                property1.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt1 == "string")
            {
                property1.Type = new CodeTypeReference(typeof(System.String));
            }

            targetClass.Members.Add(property1);

            CodeMemberField field2 = new CodeMemberField();
            field2.Attributes = MemberAttributes.Private;
            if (dt2 == "int")
            {
                field2.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt2 == "string")
            {
                field2.Type = new CodeTypeReference(typeof(System.String));
            }
            field2.Name = "_" + fld2;
            targetClass.Members.Add(field2);

            CodeMemberProperty property2 = new CodeMemberProperty();
            property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2)));
            property2.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2), new CodePropertySetValueReferenceExpression()));
            property2.Attributes = MemberAttributes.Public;
            property2.Name = fld2;
            if (dt2 == "int")
            {
                property2.Type = new CodeTypeReference(typeof(System.Int32));
            }
            else if (dt2 == "string")
            {
                property2.Type = new CodeTypeReference(typeof(System.String));
            }

            targetClass.Members.Add(property2);

            CodeMemberField field3 = new CodeMemberField();
            field3.Attributes = MemberAttributes.Private;
            
            // field3.Type = new CodeTypeReference(typeof(System.Int32));
            Type myType = Type.GetType("MVCNHibernate.Entities."+parent);
            //dynamic instance = Activator.CreateInstance(myType);

            field3.Type = new CodeTypeReference(myType);

            field3.Name = "_" + parent+tableName;
            targetClass.Members.Add(field3);

            CodeMemberProperty property3 = new CodeMemberProperty();
            property3.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + parent + tableName)));
            property3.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + parent + tableName), new CodePropertySetValueReferenceExpression()));
            property3.Attributes = MemberAttributes.Public;
            property3.Name = parent + tableName;
            Type myType2 = Type.GetType("MVCNHibernate.Entities." + parent);
           // dynamic instance2 = Activator.CreateInstance(myType2);
            property3.Type = new CodeTypeReference(myType2);

            targetClass.Members.Add(property3);
        }
   
        CodeDomProvider provider;
        public void GenerateCSharpCode(string fileName)
        {
             
             provider = CodeDomProvider.CreateProvider("CSharp");
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BracingStyle = "C";

            using (StreamWriter sourceWriter = new StreamWriter(fileName))
            {
                provider.GenerateCodeFromCompileUnit(
                    targetUnit, sourceWriter, options);
            }
        }
    }
}

GenerateMap.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Linq.Expressions;

namespace MVCNHibernate
{
    public class GenerateMap
    {
        CodeCompileUnit targetUnit;

        CodeTypeDeclaration targetClass;

        public GenerateMap(string tableName)
        {
            targetUnit = new CodeCompileUnit();
           
            //Path
            CodeNamespace samples = new CodeNamespace("MVCNHibernate.Mapping");
           
            //Namespace
            samples.Imports.Add(new CodeNamespaceImport("System"));
            samples.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            samples.Imports.Add(new CodeNamespaceImport("System.Linq"));
            samples.Imports.Add(new CodeNamespaceImport("System.Web"));
            samples.Imports.Add(new CodeNamespaceImport("FluentNHibernate.Mapping"));
            samples.Imports.Add(new CodeNamespaceImport("MVCNHibernate.Entities"));

            targetClass = new CodeTypeDeclaration(tableName+"Map");
            targetClass.BaseTypes.Add(new CodeTypeReference { BaseType = "ClassMap`1[" + tableName + "]", Options = CodeTypeReferenceOptions.GenericTypeParameter });
            targetClass.IsClass = true;
            targetClass.TypeAttributes =
                TypeAttributes.Public ;
            samples.Types.Add(targetClass);
            targetUnit.Namespaces.Add(samples);

        }

        public void AddConstructor(string fld1, string fld2, string tbl)
        {
            // Declare the constructor
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes =
                MemberAttributes.Public | MemberAttributes.Final;

            CodeExpression newType = new CodeExpression();
            CodeSnippetExpression snippet = new CodeSnippetExpression();

            string hh = string.Format("Table(\"{0}\"", tbl);
            string lambda = @"Id(x => x." + fld1 + "); Map(x => x." + fld2 + ");" + hh+")";

            var lambdaExpression = new CodeSnippetExpression(lambda);
           
            constructor.Statements.Add(lambdaExpression);

            targetClass.Members.Add(constructor);
        }


        public void RelationalAddConstructor(string fld1, string fld2, string tbl, string parent)
        {
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes =
                MemberAttributes.Public | MemberAttributes.Final;

            CodeExpression newType = new CodeExpression();
            CodeSnippetExpression snippet = new CodeSnippetExpression();

            string parenttbl = parent + tbl;
            string fk=parent+"id";
            string cc= string.Format("\"{0}\"", fk);
            string hh = string.Format("Table(\"{0}\"", tbl);
            string lambda = @"Id(x => x." + fld1 + "); Map(x => x." + fld2 + "); References(x => x." + parenttbl + ").Column(" + cc + "); " + hh + ")";

            var lambdaExpression = new CodeSnippetExpression(lambda);
            
            constructor.Statements.Add(lambdaExpression);

            targetClass.Members.Add(constructor);
        }

        public void GenerateCSharpCode(string fileName)
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BracingStyle = "C";

            using (StreamWriter sourceWriter = new StreamWriter(fileName))
            {
                provider.GenerateCodeFromCompileUnit(
                    targetUnit, sourceWriter, options);
            }
        }
    }
}

GenerateDBController.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCNHibernate.Entities;
using Microsoft.Build;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using NHibernate.Linq;
using NHibernate;
using System.Collections;

namespace MVCNHibernate.Controllers
{
    public class GenerateDBController : Controller
    {
    
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult getDataType()
        {
            using (var session = NHibernate.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var query = session.CreateSQLQuery("select Name from DataType");
                    var result = query.List();
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }
        }

        public JsonResult getTable()
        {
            using (var session = NHibernate.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var sql = String.Format("SELECT * FROM information_schema.tables");
                    var query = session.CreateSQLQuery(sql);
                    var result = query.List();
                   
                    List<string> tableName = new List<string>();
                    object tableSpec;

                    IList collection;
                    for (int i = 0; i < result.Count; i++)
                    {
                        tableSpec = result[i];
                        collection = (IList)tableSpec;
                        tableName.Add(collection[2].ToString());
                       
                    }
                  
                    return Json(tableName, JsonRequestBehavior.AllowGet);
                }
            }
        }

        public string Generator(string tableName, string fieldName1, string dataType1, string fieldName2, string dataType2, string Parent)
        {
            try
            {

            if (Parent==null) //=> No Relation
            {
                var projectCollection = ProjectCollection.GlobalProjectCollection;
                string projPath = "~/MVCNHibernate.csproj";

                var p = projectCollection.LoadProject(Server.MapPath(projPath));


                string projItem1 = "~/Entities/" + tableName + ".cs";
                GenerateTable genTable = new GenerateTable(tableName);
                genTable.AddFields( fieldName1,  dataType1,  fieldName2,  dataType2);
                genTable.GenerateCSharpCode(Server.MapPath(projItem1));

                p.AddItem("Compile", Server.MapPath(projItem1));
                p.Save();

                string projItem2 = "~/Mapping/" + tableName + "Map.cs";
                GenerateMap genMap = new GenerateMap(tableName);
                genMap.AddConstructor(fieldName1,fieldName2,tableName);
                genMap.GenerateCSharpCode(Server.MapPath(projItem2));

                p.AddItem("Compile", Server.MapPath(projItem2));
                p.Save();
                ProjectCollection.GlobalProjectCollection.UnloadProject(p);

                p.Build();
               
                NHibernate.OpenSession();
                              
            }
            else if (Parent != null)//=> Relation To Parent
            {
                var projectCollection = ProjectCollection.GlobalProjectCollection;
                string projPath = "~/MVCNHibernate.csproj";

                var p = projectCollection.LoadProject(Server.MapPath(projPath));


                string fileNameEn = "~/Entities/" + tableName + ".cs";
                GenerateTable genTable = new GenerateTable(tableName);
                genTable.RelationalAddFields(tableName, fieldName1, dataType1, fieldName2, dataType2, Parent);
                genTable.GenerateCSharpCode(Server.MapPath(fileNameEn));

                string projItem1 = "~/Entities/" + tableName + ".cs";
                p.AddItem("Compile", Server.MapPath(projItem1));
                p.Save();

                string fileNameMap = "~/Mapping/" + tableName + "Map.cs";
                GenerateMap genMap = new GenerateMap(tableName);
                genMap.RelationalAddConstructor(fieldName1, fieldName2, tableName, Parent);
                genMap.GenerateCSharpCode(Server.MapPath(fileNameMap));

                string projItem2 = "~/Mapping/" + tableName + "Map.cs";
                p.AddItem("Compile", Server.MapPath(projItem2));
                p.Save();
                //ProjectCollection.GlobalProjectCollection.UnloadProject(p);
                ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
                p.Build();
               
                NHibernate.OpenSession();

              
            }
            return "Database generated Successfully ";
              
            }
            catch
            {
                
                return "Database did not generate Successfully ";
            }
                      
        }

    }
}
</string></string>

Entities -> DataType.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCNHibernate.Entities
{
    public class DataType
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
    }
}

Mapping -> DataTypeMap.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentNHibernate.Mapping;
using MVCNHibernate.Entities;

namespace MVCNHibernate.Mapping
{
    public class DataTypeMap : ClassMap<datatype>
    {
        public DataTypeMap()
        {
            Id(x => x.ID);

            Map(x => x.Name);
           
            Table("DataType");
        }
    }
}</datatype>

Content -> Angular -> Module.js

var app = angular.module('MyApp', ['ngRoute']);

Content -> Angular -> Controller.js

app.controller("AngularCtrlCreateTable", function ($scope, angularServiceCreateTable) {
    $scope.divGen = true;
    $scope.divNewGen = false;
    GetAllDataType1();
    GetAllTable();

    //To Get All Records  
    function GetAllDataType1() {
        var Data = angularServiceCreateTable.getDataType();
        Data.then(function (data) {
            $scope.items1 = data.data;
            $scope.items2 = data.data;
        }, function () {
            alert('Error');
        });
    }

     
    //To Get All Records  
    function GetAllTable() {
        var Data = angularServiceCreateTable.getTable();
        Data.then(function (tb) {
            $scope.itemsPT = tb.data;
        }, function () {
            alert('Error');
        });
    }

    $scope.Generator = function () {
        var dt1 = $scope.selectedItem1;
        var dt2 = $scope.selectedItem2;
        var dt3 = $scope.selectedItemParentTable;
        var getmsg = angularServiceCreateTable.Generator($scope.TableName, $scope.Field1, dt1, $scope.Field2, dt2, dt3);
        getmsg.then(function (messagefromcontroller) {
            $scope.divGen =false;
            $scope.divNewGen = true;
           alert(messagefromcontroller.data);
        }, function () {
            alert('There is error in database generation');
        });
    }

    $scope.GeneratorNew = function () {
            $scope.divGen = true;
            $scope.divNewGen = false;
            GetAllTable();
            GetAllDataType2();
            GetAllDataType1();
            $scope.TableName = "";
            $scope.Field1 = "";
            $scope.Field2 = "";
    }

});

Content -> Angular -> Service.js

app.service("angularServiceCreateTable", function ($http) {
    this.getDataType = function () {
        return $http.get("/GenerateDB/getDataType");
    };

    this.getTable = function () {
        return $http.get("/GenerateDB/getTable");
    };
   
    //Database Generation 
    this.Generator = function (tblName, fldName1, dType1, fldName2, dType2, prtTableName) {
        var response = $http({
            method: "post",
            url: "/GenerateDB/Generator",
            params: {
                tableName: tblName,
                fieldName1: fldName1,
                dataType1: dType1,
                fieldName2: fldName2,
                dataType2: dType2,
                Parent: prtTableName
            }
        });
        return response;
    }

});

Views -> GenerateDB -> Index.cshtml

@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<div ng-controller="AngularCtrlCreateTable">
    <br />
    <table>
            <tr>
                  <td>
                     Table Name:
                     <input type="text" style="width:94px;" ng-model="TableName" />
                 </td>
             </tr>

             <tr>
                 <td>
                     Field1(PK):<input type="text" style="width:94px;" ng-model="Field1" />
                     Data Type:<select data-ng-model="selectedItem1" ng-options="item1 for item1 in items1"></select>
                 </td>
             </tr>

             <tr>
                 <td>
                     Field2: <input type="text" style="width:94px;" ng-model="Field2" />
                     Data Type: <select data-ng-model="selectedItem2" ng-options="item2 for item2 in items2"></select>

                 </td>
             </tr>

             <tr>
                 <td>
                    Relationship to Parent Table(FK): <select data-ng-model="selectedItemParentTable" ng-options="itemPT for itemPT in itemsPT"></select>
                 </td>
             </tr>

             <tr>
                 <td colspan="2">
                     <div ng-show="divGen">
                         <input type="button" value="Generate Table" ng-click="Generator()" />
                     </div>
                </td>

                 <td colspan="2">
                     <div ng-show="divNewGen">
                         <input type="button" value="Generate New Table" ng-click="GeneratorNew()" />

                      </div>
                </td>
             </tr>

        </table>
</div>

References:

http://nhibernate.info/doc/nh/en/

http://www.codeguru.com/vb/gen/article.php/c19573/Microsoft-NET-CodeDom-Technology.htm

http://ayende.com/blog/4351/nhibernate-vs-entity-framework-4-0

http://www.codeproject.com/Articles/148959/How-the-new-C-dynamic-type-can-simplify-access-to

http://technical.cosmicverse.info/article/index/3

http://www.codeproject.com/Articles/869433/AngularJS-MVC-Repository-Dispose?msg=5029301#xx5029301xx

History

First Version: 03/28/2015

Feefback

Feel free to leave any feedback on this article; it is a pleasure to see your comments and vote about this code. If you have any questions, please do not hesitate to ask me here.