Get System.Data.Linq.Mapping.MetaModel from any database

936 Views Asked by At

I am developing an app that has to do some work with the InformationSchema of SQL Server databases. So far, I am able to retrieve that information by doing the following:

            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();

            SqlCommand sqlCmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES", conn);

            SqlDataReader dataReader = sqlCmd.ExecuteReader();

            DataTable dataTable = new DataTable();
            dataTable.Load(dataReader);

This works as expected. But this way I have to be working with DataTables which is not too convenient to me.

So, after some research I found that by using Linq to Sql one can be able to obtain a System.Data.Linq.Mapping.MetaModel which basically contains all the info about tables and columns of the database:

        MyDatabaseDataContext context = new MyDatabaseDataContext (connection);

        var tables = context.Mapping.GetTables();

        // do some work

The problem for me is that apparently that info is only retrieved when the mappings are specified in the code:

    // these lines of code are generated by the sqlmetal.exe tool
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id
    {

So, for that reason I cannot retrieve that by doing this:

        System.Data.Linq.DataContext context = new System.Data.Linq.DataContext(conn);

        var tables = context.Mapping.GetTables();

Beacuse it returns nothing, even when the database does have tables.

So, Is there a way to retrieve a System.Data.Linq.Mapping.MetaModel for any database without generating its custom DataContext??

Thanks in advance.

0

There are 0 best solutions below