This is a migrated thread and some comments may be shown as answers.

Use OpenAccess for scanning tables and columns of database

2 Answers 31 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Olivier
Top achievements
Rank 1
Olivier asked on 13 Mar 2015, 01:23 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Zhivkov
Telerik team
answered on 18 Mar 2015, 11:46 AM
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.
 
0
Olivier
Top achievements
Rank 1
answered on 18 Mar 2015, 01:13 PM
Hello Zhivkov
YES ! That's what I want.
Thanks very much
Tags
Development (API, general questions)
Asked by
Olivier
Top achievements
Rank 1
Answers by
Viktor Zhivkov
Telerik team
Olivier
Top achievements
Rank 1
Share this question
or