Currently LoBDb.Net supports only a programmatic way of defining the database schema. The following example shows how to create the schema for storing students and groups. The current user has to have the permission to change the schema in order to run the example.

using System;
using Centido.Core;

class DomainSample
{
    static void Main()
    {
        var domainConfig = new DomainConfiguration()
        {
            DbConnectionString = @"Driver={SQL Server};Server=.\SQLEXPRESS;Database=TestDb;Uid=MyAdmin;Pwd=P@ssw0rd;",
            DbProviderName = "MSSQLSERVER"
        };

        var domain = Domain.Build(domainConfig);

        // Deleting the old schema
        domain.DeleteAllTables();

        // Creating tables for students and groups
        var groupTable = domain.AddTable("groups");
        var studentTable = domain.AddTable("students");

        // Creating the columns of the first table
        // The group name
        var groupName = groupTable.AddColumn("name", ColumnType.String) as StringColumn;
        groupName.IsNotNull = true;
        groupName.IsNotEmpty = true;
        groupName.DefaultValue = "<Empty>";
        groupName.MaxLength = 80;

        // Creating the columns of the second table
        // The student name
        var studentName = studentTable.AddColumn("name", ColumnType.String) as StringColumn;
        studentName.IsNotNull = true;
        studentName.IsNotEmpty = true;
        studentName.DefaultValue = "<Empty>";
        studentName.MaxLength = 80;

        // The student gender
        var studentGender = studentTable.AddColumn("gender", ColumnType.Enumeration) as EnumColumn;
        studentGender.IsNotNull = true;

        studentGender.AddMember(0).DisplayName = "Male";
        studentGender.AddMember(1).DisplayName = "Female";

        studentGender.DefaultValue = 0;

        // The group reference
        var studentGroup = studentTable.AddColumn("group_id", ColumnType.Reference) as RefColumn;
        studentGroup.IsNotNull = true;
        
        studentGroup.AddTarget(groupTable).CascadeDelete = true;

        // Displaying the schema
        foreach(var table in domain.Tables)
        {
                Console.WriteLine("Table: name = {0}", table.Name);
                
                foreach(var column in table.Columns)
                        Console.WriteLine("\tColumn: name = {0}, type = {1}", column.Name, column.ColumnType);
        }

        // The index on the reference column
        studentTable.AddIndex(studentGroup);
    }
}

Last edited Mar 8, 2012 at 6:18 PM by mapase, version 18

Comments

No comments yet.