Use OpenAccess for scanning tables and columns of database

3 posts, 1 answers
  1. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 13 Mar 2015 Link to this post

    Hello
    I would develop my own code generator and I would use OpenAccess for retreving description of tables/columns on an database
    I tried this
    Telerik.OpenAccess.BackendConfiguration backend = new Telerik.OpenAccess.BackendConfiguration();
    backend.Backend = "PostgreSql";
    backend.ProviderName = "Npgsql";
    Telerik.OpenAccess.Metadata.MetadataContainer metaData = new Telerik.OpenAccess.Metadata.MetadataContainer();
    metaData.DefaultMapping.InheritanceStrategy = Telerik.OpenAccess.InheritanceStrategy.Horizontal;
                 
    using (Telerik.OpenAccess.OpenAccessContext db = new Telerik.OpenAccess.OpenAccessContext("Connexion", backend, metaData))
     {           
        List<string> listTables = new List<string>();
        foreach (var table in db.Metadata.Tables) listTables.Add(table.Name);
     
        listBox.ItemsSource = listTables;
    }
    But Metadata always blank.
    Can you help me ?
    Thanks
  2. Answer
    Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 18 Mar 2015 Link to this post

    Hi Arnaud,

    You can read the real schema from the database using our Schema Read API.
    The way you have tried to do it will return only the tables and columns that are already mapped in your model and not the ones found in the database.

    Here is an example how to obtain all tables in a database, how to get information about columns and how to determine if a column participates in the primary key:
    01.using(DbConnection dbConnection = /* new or existing connection to your database */)
    02.{
    03.    // obtain reader
    04.    DbSchemaReader reader = DbSchemaReader.Create(dbConnection, OpenAccess.Metadata.Backend.PostgreSql);
    05.    // configure the reader if necessary
    06.    // execute the schema read operation
    07.    reader.Execute();
    08. 
    09.    // process the results
    10.    foreach (var table in reader.RelationalTables)
    11.    {
    12.        foreach (var column in table.Value.cols)
    13.        {
    14.            if (column.pk) // indicates that the column is primary key
    15.            {
    16.                // handle primary key columns
    17.            }
    18.            // handle normal columns
    19.        }
    20.    }
    21.}

    DbSchemaReader class i found in Telerik.OpenAccess.Runtime.Schema namespace.

    If you need any further assistance you are welcome to post your questions here or in a new forum thread.

    Regards,
    Viktor Zhivkov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 18 Mar 2015 in reply to Viktor Zhivkov Link to this post

    Hello Zhivkov
    YES ! That's what I want.
    Thanks very much
Back to Top